Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

78 users online



PHP / MySQL script not working

PHP / MySQL script not working

Currently viewing this thread: 1 (0 members and 1 guests)


not_taken

not_taken

Status: Offline!

PHP / MySQL script not working

I am new to databases and I have put this code together

PHP:

<?
$today 
date("Y-m-d"); // Date using PHP
?>

<center><b>Submit News</b></center>

<form action= "<?=$PHP_SELF?>" method= "post">
Insert title here:<br>
<input type= "text" name= "title"><p>
Date:<br>
<input type="text" name="date" value="<? echo($today); ?>"><p>
Insert text here:<br>
<textarea name="text" rows="20" cols="100"></textarea><p>
<input type="submit" value="Submit News" name="submitnews"> &nbsp;
<input type="reset" value="Clear all fields"><p>

<?

$database 
mysql_connect ("localhost""***""***"// Connect to database server
or die ("Could not connect to database server");         // Prints Could not connect to database server if connect fails.
echo ("Connected to database server<br>");             // Prints connected to database server in connection is succesfull

mysql_select_db ("***")                     // Connect to database
or die ("Could not connect to database");             // Prints Could not connect to database if connect fails    
echo ("Connected to database<br>");                 // Prints Connected to database if connect suceeds

if ($submitnews)                     // Checks to see if the Submit button has been clicked on        
{
$sqlMYSQL_QUERY ("INSERT INTO site_news(id,title,text,date)".
"VALUES ('NULL', '$title', '$text', '$today')");         // Inserts the data into the table

if (mysql_query($sql))
{
echo(
"News added to database");
}
else
{
echo(
"Error adding news to database");

}
}

?>

As you have probably figured out, it is supposed to connect to the database and insert whatever i put in the form into the table site_news. It seems to connect to the database but it says "Error adding news to database" What is wrong with the code?

Also how waould I make a page displaying the contents of the table, and would HTML code work if I put it into the form?

Thanks in advance

Last edited by not_taken, August 6th, 2003 03:31 PM (Edited 1 times)

Phil

Phil

with Mr. Jones
Status: Offline!

Wow... these new hacks are nice

do

PHP:

<?php

mysql_query
($sql) or die(mysql_error());

?>

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

JustEvolved

JustEvolved

Status: Offline!

try this

PHP:

<?php

$sql
MYSQL_QUERY ("INSERT INTO site_news(title, text, date)".
"VALUES ('$title', '$text', '$today')");
mysql_query($sql) or die(mysql_error());

?>

You dont have to send a value for id if you set id up to auto increment then the database will add the next number.

let me know if it works.

___________________

We are survival machines - robot vehicles blindly programmed to preserve the selfish molecules known as genes.

- Richard Dawkins

IdleNut

IdleNut

Neverside Newbie
Status: Offline!

just a note that <form action= "<?=$PHP_SELF?>"> is the same as <form action= ""> Wink

evan

evan

Neverside Newbie
Status: Offline!

I would use [color=red]$_SERVER['PHP_SELF'][/color]though. Not [color=red]$PHP_SELF [/color]

Last edited by evan, August 6th, 2003 06:58 PM (Edited 1 times)

not_taken

not_taken

Status: Offline!

Ok, thanks for all the help, I woas looking at the table I createdin phpMyAdmin and it sem like the code was working all along, thanks anyway!

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0085 seconds.