Page 1 of 1
mysql_query
Posted: Sun Jun 29, 2003 2:03 am
by Drachlen
Heyo.. I'm having a bit of trouble using mysql_query... I'm not getting an error, its just giving me the die message. Here is the piece of code that i think is the problem:
Code: Select all
<?php
mysql_query("INSERT INTO test (username, password, email, rank) VALUES ('$username', '$password', '$email', '$rank') WHERE id='$id'", $link) or die("Could not write information");
?>
Im trying to write the information into the row which has the id number of the account i was modifying, so i put WHERE id='$id' to tell it where to place it. Im not sure if im misusing it or not, but id appeciate it if someone could fix this.
Posted: Sun Jun 29, 2003 5:57 am
by Zeceer
Try this:
mysql_query("INSERT INTO test (username, password, email, rank) VALUES ('".$username."', '".$password."', '".$email."', '".$rank."') WHERE id='$id'", $link) or die("Could not write information");
Basically use this syntax: '".$username."' instead of '$username'.
Posted: Sun Jun 29, 2003 7:01 am
by Drachlen
Same thing happens, heres my entire source:
Code: Select all
<?php
echo "<a href=show.php>Back</a><br>";
$_POST['username'] = addslashes($_POST['username']);
$_POST['password'] = addslashes($_POST['password']);
$_POST['email'] = addslashes($_POST['email']);
$link = mysql_connect("localhost", "Drachlen", "*")
or die("Could not connect");
mysql_select_db("game", $link)
or die("Could not select database");
mysql_query("INSERT INTO test (username, password, email, rank) VALUES ('".$username."', '".$password."', '".$email."', '".$rank."') WHERE id='$id'", $link) or die("Could not write information");
mysql_close($link);
?>
and i dont get any errors, just the die message... "Could not write information" =/
Posted: Sun Jun 29, 2003 7:26 am
by Zeceer
Where does the $id variable comes from? If this is sent from a link at the page before the script, you must retrieve the information with $_GET['id'] if your global variables are turned off.
If this doesn't solve you problem, please post some of the code from the document before this script.
+ in the your die sentence you can input mysql_error(). This will output more detailed info about whats wrong to the browser.
Code: Select all
die( "Could not write information". mysql_error() );
Posted: Sun Jun 29, 2003 7:50 am
by Drachlen
You have an error in your SQL syntax near 'WHERE (id='14')' at line 1
This is what its giving me now, i know the ID is passing because its displaying the number.... I specifically need that part so it knows which row to modify, because without it, it just creates a new account.. Do you think it would just be simpler to delete the old row and remake it as a new one? Yea.. im going to try that..
Posted: Sun Jun 29, 2003 8:09 am
by Drachlen
Okay i got it working. It wasnt the way i originally planned, but it works, and thats what matters. Heres the code for anyone who also wanted to know:
Code: Select all
<?php
header("location: show.php");
$_POST['username'] = addslashes($_POST['username']);
$_POST['password'] = addslashes($_POST['password']);
$_POST['email'] = addslashes($_POST['email']);
$link = mysql_connect("localhost", "Drachlen", "*")
or die("Could not connect");
mysql_select_db("game", $link) or die("Could not select database");
$a = "DELETE FROM test WHERE id=$id";
$b = mysql_query($a);
mysql_query("INSERT INTO test (username, password, email, rank, id) VALUES ('$username', '$password', '$email', '$rank', '$id')", $link) or die( "Could not write information. Reason: ". mysql_error() );
mysql_close($link);
?>
Posted: Sun Jun 29, 2003 8:13 am
by SBukoski
It looks like you're trying to update an existing record. The INSERT command will try to insert a new row into the table. Try using the UPDATE command instead. For example:
Code: Select all
mysql_query("UPDATE test SET username='$username', password='$password', email='$email', rank='$rank' WHERE id='$id'", $link) or die("Could not write information");