...

View Full Version : simple syntax error



thesavior
01-08-2006, 02:31 AM
mysql_query("update $tbl_name set firstname=$fname, lastname=$lname where firstname=$firstname and lastname=$lastname")
or die(mysql_error());
echo "Successful!";

Is returning:


syntax error near 'and lastname=' at line 1

thesavior
01-08-2006, 05:35 PM
are my variables supposed to be in some sort of quotes?

Kid Charming
01-08-2006, 07:39 PM
Strings should be single-quoted. Integers don't have to be.

thesavior
01-08-2006, 08:29 PM
why isn't this actually updating my information? Everything should be correct, but nothing is happening.


<?php
mysql_connect("$host", "$dbusername", "$dbpassword")or die(mysql_error());
mysql_select_db("$dbname")or die(mysql_error());
$step = $_GET['step'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
if ($step == selection) {
if ($fname == "" || $lname == "") {
echo "No fields can be left empty.";
exit;
}
mysql_query("update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'")
or die(mysql_error());
echo "Successful!";
exit;
}
if ($step == retrieve) {
$select = mysql_fetch_array(mysql_query("select * from $tbl_name where firstname='$firstname' && lastname='$lastname'"));
if (!$select) {
echo "Not a good name.";
exit;
}
echo "Hello <b>$firstname $lastname</b>!<br>";
echo "<form method=post action=?step=selection>";
echo "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
echo "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
echo "<input type=\"submit\" value=\"Submit\" /></form>";
exit;
}
?>
<form method="post" action="?step=retrieve">
First name:<input type="text" name="firstname" /><br>
Last Name: <input type="text" name="lastname" /><br>
<input type="submit" value="Submit" /></form>

It says "Successfull" but nothing is actually changed.

Kid Charming
01-08-2006, 09:02 PM
When using variables in queries, I prefer to set the query into a separate variable so it's easier to echo and see exactly what you're sending to the db:



$query = "update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'";
mysql_query($query) or die(mysql_error());


Now you can echo $query and see what you're variables are being set as, and that can help with your debugging.

thesavior
01-09-2006, 01:10 AM
ok, the query is just this:


update bsa_scouts set firstname='---', lastname='-----' where firstname='' and lastname=''

i know that it just isn't getting any info for the firstname and lastname original so it doesn't know where to edit, but im not sure how to fix my code to work.


<? include '../includes/config.inc.php'; ?>
<? include '../includes/func.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="../stylesheet.css"/>
<title>Edit Information</title>
</head>

<body>
<div class="wrapper">
<div class="title">
<img class="title_image" src="../flourdeleis_wood.png" alt="Header Image" width="180px" height="150px"/>
Edit Information
</div>
<? include 'includes/memnav.inc.php';?>
<div class="main">
<?php
mysql_connect("$host", "$dbusername", "$dbpassword")or die(mysql_error());
mysql_select_db("$dbname")or die(mysql_error());
$step = $_GET['step'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
if ($step == selection) {
if ($fname == "" || $lname == "") {
echo "No fields can be left empty.";
exit;
}
$updatequery = "update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'";
mysql_query($updatequery)
or die(mysql_error());
echo "Successful!".$updatequery;
exit;
}
if ($step == retrieve) {
$select = mysql_fetch_array(mysql_query("select * from $tbl_name where firstname='$firstname' && lastname='$lastname'"));
if (!$select) {
echo "Not a good name.";
exit;
}
echo "Hello <b>$firstname $lastname</b>!<br>";
echo "<form method=post action=?step=selection>";
echo "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
echo "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
echo "<input type=\"submit\" value=\"Submit\" /></form>";
exit;
}
?>
<form method="post" action="?step=retrieve">
First name:<input type="text" name="firstname" /><br>
Last Name: <input type="text" name="lastname" /><br>
<input type="submit" value="Submit" /></form>
</div>
<div class="copyright">
<?=$copyright;?>
</div>
</div>

</body>
</html>

Kid Charming
01-09-2006, 03:47 AM
You're looking for the elements 'fname' and 'lname' in your POST array, but your form doesn't have any 'fname' or 'lname' fields -- you'll need to add them.

thesavior
01-09-2006, 06:00 AM
it does have fname and lname.

There are two forms, this is the second one:


echo "Hello <b>$firstname $lastname</b>!<br>";
echo "<form method=post action=?step=selection>";
echo "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
echo "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
echo "<input type=\"submit\" value=\"Submit\" /></form>";
exit;

Kid Charming
01-09-2006, 04:28 PM
My mistake -- you're missing $firstname and $lastname.

The first form defines them and brings up the second form. But when you submit the second form, you lose the values for $firstname and $lastname. POST data does not carry over from multiple form submissions. You need to either use sessions to keep those values or place them in your second form as hidden fields. The second option is easier and will probably work fine for you.

thesavior
01-09-2006, 05:16 PM
...okay...so how do i do that...?

lol :cool:

Kid Charming
01-09-2006, 06:11 PM
Read this thread. (http://www.codingforums.com/showthread.php?t=2635)

thesavior
01-09-2006, 11:39 PM
doing hidden fields like that however, means that someone can download the form, change the html, and reupload it to wherever they are, and run it. I want it to be php.

Kid Charming
01-10-2006, 12:57 AM
Then use a session. (http://us3.php.net/session) I think we've ventured pretty far outside MySQL, though, so any session questions should be raised in the PHP forum.

thesavior
01-10-2006, 01:34 AM
i pmed a moderator and hopefully this will just be moved.

Can you explain how to add sessions to my code? I know what a session generally is, but im not completely sure how to use them.

thesavior
01-13-2006, 12:28 AM
*bump*

Velox Letum
01-13-2006, 01:12 AM
Google.com. Tutorials on Sessions.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum