PDA

View Full Version : What's wrong with this PHP/MySQL script?


KyleEW12
12-03-2002, 04:06 AM
$db = mysql_connect("localhost", "kwilli_kwilliams", "******");

mysql_select_db("kwilli_veritas",$db);

$sql="INSERT INTO users (login, password, name, phone, description, email, column, id) VALUES ('$login', '$password', '$name', '$phone', '$information', '$email', '$column', '$id')";

$result=mysql_query($sql,$db);


The form below posts to the script above.


<form action="admin.post.php" method="post">
<table cellpadding="2" cellspacing="0" border="1" width="387">
<tr><td><font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="2"><b>Name</b></font></td><td><input type="text" name="name" size="28"></td></tr>

<tr><td><font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="2"><b>Username</b></font></td><td><input type="text" name="login" size="28"></td></tr>

<tr><td><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Password</b></font></td>
<td><input type="password" name="password" size="24"></td> <tr><td>

<font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Email</b></font></td><td><input type="text" name="email" size="28"></td><tr>
<td><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Phone</b></font></td>
<td><input type="text" name="phone" size="28"></td>

<tr><td><b><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Information</font></b></td><td><textarea name="information" cols="25" rows="4"></textarea></td>
<tr>
<td><b><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">Column</font></b></td>
<td><input type="text" name="column" size="28"></td>
<tr><td><font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>Add</b></font></td><td><input type="hidden" value="2" name="id"><input type=submit value="Add to Database"></td></tr>
</table></form>



There are no errors, but it just doesn't post into the database.

Can you find any problems with it?


Any help is appreciated.

mat
12-03-2002, 04:28 AM
wow is this deja vu to anyone else :eek: :confused:

anyway, why not try:



db = mysql_connect("localhost", "kwilli_kwilliams", "******");

mysql_select_db("kwilli_veritas");

$sql="INSERT INTO users (login, password, name, phone, description, email, column, id) VALUES ('$login', '$password', '$name', '$phone', '$information', '$email', '$column', '$id')";

$result=mysql_query($sql);

KyleEW12
12-03-2002, 04:39 AM
Parse error: parse error, unexpected '=' in /home/vsites/kwilli/public_html/update/admin.post.php on line 9


line 9 is below:

db = mysql_connect("localhost", "kwilli_kwilliams", "*******");

mat
12-03-2002, 04:59 AM
try just using:


mysql_connect('localhost', 'kwilli_kwilliams', '******');


that makes more sense in regards to my previous reply anyway


so it becomes..



mysql_connect('localhost', 'kwilli_kwilliams', '******');
mysql_select_db("kwilli_veritas");

$sql="INSERT INTO users (login, password, name, phone, description, email, column, id) VALUES ('$login', '$password', '$name', '$phone', '$information', '$email', '$column', '$id')";

$result=mysql_query($sql);

KyleEW12
12-03-2002, 05:24 AM
Same result as the old version. The script runs through without errors, but fails to post anything to the database.


Anymore ideas?

mat
12-03-2002, 05:44 AM
hmmm not really..

Although you are accessing the form variables using $varible_name_here.
this may not always work depending on the setup, version of PHP running. see below:



// Available if the PHP directive register_globals = on.
// As of PHP 4.2.0 the default value of register_globals = off.
// Using/relying on this method is not preferred.

print $username

// Available since PHP 3.

print $HTTP_POST_VARS['username'];
$_POST['variable'];

// Available since PHP 4.1.0

print $_POST['username'];
print $_REQUEST['username'];

Wichetael
12-03-2002, 11:02 AM
Check to see if you get an error from MySQL like so:

mysql_connect('localhost', 'kwilli_kwilliams', '******');
mysql_select_db("kwilli_veritas");

$sql="INSERT INTO users (login, password, name, phone, description, email, column, id) VALUES ('$login', '$password', '$name', '$phone', '$information', '$email', '$column', '$id')";

$result=mysql_query($sql);

if (mysql_errno())
echo 'Error: '.mysql_error();

That should hopefully give you some answers also, try echoing the SQL query to see if there are any errors:

echo 'SQL: '.$sql;

mbenson
12-03-2002, 02:58 PM
I like


$result=mysql_query($sql);

if (!$result) {
echo("<p>Error in query: " . mysql_error() . "</p>");
exit();
}


just preference though..

KyleEW12
12-03-2002, 05:06 PM
The script didn't work because the words "password" and "column" are two SQL hot words. I suppose they're some sort of function.


Anyway, I changed them to "password1" and "column1" and it works fine. Thanks for the help.

Wichetael
12-04-2002, 01:13 PM
Of course, password is an SQL function for one way encryption of passwords, don't know about column though, prolly just a reserved word...

mat
12-04-2002, 10:25 PM
I've used password as a column name before (mySQL), it woked :confused:

KyleEW12
12-04-2002, 10:48 PM
Maybe you have a different version of MySQL. I'm running MySQL 3.23.53a.