mysql_query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

mysql_query

Post 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.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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'.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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" =/
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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() );
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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..
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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);


?>
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post 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");
Post Reply