Jump to content

[SOLVED] UPDATE HELP


jmr3460

Recommended Posts

MySQL version -- MySQL 5.0.51a

   

* the raw MySQL statement in question

[query('UPDATE james SET name=$name,age=$age,email=$email,comment=$comment WHERE id=.$_POST['id]);]

   

* any errors that MySQL returns to the client

[uPDATE james SET name=$name,age=$age,email=$email,comment=$comment WHERE id=

Query failed: error# 1064 -- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]

[i am afraid that the $_POST['id'] is the problem. I am trying this to pick my WHERE id=.]

   

* the table structure & column indexes of the relevant tables

[CREATE TABLE james (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR,age VARCHAR,email VARCHAR,comment VARCHAR) VALUES(some,values) ]

   

* the EXPLAIN output for your query, if applicable

[The statement I am trying to make is to update a beginning of a contact form. The script opens the Database and will INSERT data, DELETE data, it will open an UPDATE form, but when I try to UPDATE the UPDATE form I get the previous error]

    * a clear and concise description of what you want this statement to achieve UPDATE an UPDATE form]

    * a description of what it's currently doing that's not to your liking[Not updating form}

    * a brief listing of the types of things you've attempted so far

[i am very new to MySQL, I have only been studying MySQL for a couple of weeks. I only know what I have read in the books and some 30 minutes of video tutorial. I have learned more from this forum that the video tutorial. My ultimate goal is to create some web forms that any old joe can update on their own, and I was hoping to start a contact database for all of my contacts. I am very interested in the Idea of Databases.]

 

I would be very grateful if someone could help me,

jmr3460

Link to comment
https://forums.phpfreaks.com/topic/149027-solved-update-help/
Share on other sites

Let me retry this. I know that the post above is a little hard to read. This is the block that has the error.

<?php	



$id = NULL;
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$comment = $_POST['comment'];



if (isset($_POST['submit'])){
   if ($_POST['submit'] == 'Save'){
      query("INSERT INTO james(name,age,email,comment) VALUES ('$name','$age','$email','$comment')");
   }else if ($_POST['submit'] == 'Save Edits'){
   		query("UPDATE james SET name='$name',age='$age',email='$email',comment='$comment' WHERE id='.$_POST['id']'");
  }
} 

if (isset($_GET['action'])){
if ($_GET['action'] == 'delete'){
	query('DELETE FROM james WHERE id='.$_GET['id']);
	} else if ($_GET['action'] == 'edit'){
		$infos = query("SELECT * FROM james WHERE id=".$_GET['id']);
		while($info = mysql_fetch_assoc($infos)){
	$info_name = $info['name'];
	$info_age = $info['age'];
	$info_email = $info['email'];
	$info_comment = $info['comment'];
	$info_id = $info['id'];

print('<form method="POST" action="index.php">
Name: <input type="text" name="name" value="'.$info_name.'"/><br/>
Age: <input type="text" name="age" value="'.$info_age.'" /><br/>
Email: <input type="text" name="email" value="'.$info_email.'" /><br/>
Comment: <input type="text" name="comment" value="'.$info_comment.'" />
<input type="submit" name="submit" value="Save Edits" />
</form>');
}
}

}
?>

This is the error I get with this block:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/simplic5/public_html/php/index.php on line 62

 

line 62 is the UPDATE line.

 

The UPDATE WHERE is the line that has the error. When I try and edit an entry I click on edit and my edit form opens with the data in the fields correctly. I make the changes and click "Save Edits" and I get the error. Can anyone help.

jmr3460

Link to comment
https://forums.phpfreaks.com/topic/149027-solved-update-help/#findComment-782665
Share on other sites

I just got my update form to work. The problem was I did not have one of my columns added in my print form. I added a hidden id column. Now my form only has about 7 notices that say that I have undefined indexes. These only come up when I click my edit link. How important is this?

 

I am learning that it is very important to learn to read error statements. This is my whole:

<?php
$host = 'localhost';
$username = 'root';
$password = '';
$database ='contacts';
mysql_connect($host, $username, $password);
mysql_select_db($database);


$mysql_query_error = '';

function query($mysql_query){
	global $mysql_query_error;
		$data = mysql_query($mysql_query);
		$mysql_query_error .= $mysql_query.'<br/>';
		if (mysql_errno() !=0){
			$mysql_query_error .= '<strong>Query failed: error# '.mysql_errno(). ' -- '  .mysql_error().'</strong><br/>';
		}
		return $data;
		}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="keywords" content="XTML 1.0, simplicityworks.org"/>
<meta name="Discription" content="Simple XHTML page for SimplicityWorks PHP"/>
<meta name="Author" content="Mark R."/>
<title>SimplicityWorks PHP</title>
<link rel="stylesheet"  type="text/css" href="css/php.css" title="stylesheet"/>
<style type="text/css">
td {padding:5px;border:solid 1px black;}
</style>
   <!--}else if ($_POST['submit'] == 'Save Edits'){-->
</head>
<body>



<?php

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$postId = $_POST['id'];

if (isset($_POST['submit'])){
   if ($_POST['submit'] == 'Save'){
      query("INSERT INTO members(fname,lname,address,city,state,zip) VALUES ('$fname','$lname','$address','$city','$state','$zip')");
   }else if(isset($_POST['submit'])){
   if ($_POST['submit'] == 'Save Edits'){
   query("UPDATE members SET fname='$fname',lname='$lname',address='$address',city='$city',state='$state',zip='$zip' WHERE id='$postId'");
  }
}
} 

	if (isset($_GET['action'])){
	if ($_GET['action'] == 'delete'){query('DELETE FROM members WHERE id='.$_GET['id']);
	} else if ($_GET['action'] == 'edit'){

	$infos = query("SELECT * FROM members WHERE id=".$_GET['id']);
	while($info = mysql_fetch_assoc($infos)){

$info_fname = $info['fname'];
$info_lname = $info['lname'];
$info_address = $info['address'];
$info_city = $info['city'];
$info_state = $info['state'];
$info_zip = $info['zip'];
$info_id = $info['id'];



		print('<form method="POST" action="index.php">
		<input type="hidden" name="id" value="'.$info_id.'"/>
		<table>
		<tr><td>First Name:</td><td><input type="text" name="fname" value="'.$info_fname.'"/></td></tr>
		<tr><td>Last Name:</td><td><input type="text" name="lname" value="'.$info_lname.'"/></td></tr>
		<tr><td>Address:</td><td><input type="text" name="address" value="'.$info_address.'"/></td></tr>
		<tr><td>City:</td><td><input type="text" name="city" value="'.$info_city.'" /></td></tr>
		<tr><td>State:</td><td><input type="text" name="state" value="'.$info_state.'" /></td></tr>
		<tr><td>Zip:</td><td><input type="text" name="zip" value="'.$info_zip.'" /></td></tr></table>
		<input type="submit" name="submit" value="Save Edits" />
		</form>');
			}
		}
	}
?>

<pre>


			<form method="POST" action="index.php">
				<table>
					<tr><td>First Name:</td><td><input type="text" name="fname" /></td></tr>
			<tr><td>Last Name:</td><td><input type="text" name="lname" /></td></tr>
			<tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
			<tr><td>City:</td><td><input type="text" name="city" /></td></tr>
			<tr><td>State:</td><td><input type="text" name="state" /></td></tr>
			<tr><td>Zip:</td><td><input type="text" name="zip" /></td></tr>
			<tr><td>Comment:</td><td><textarea name="comment" style="border:solid 2px black;"></textarea></td></tr>
		</table>	
			<input type="submit" name="submit" value="Save" />
		</form>		

</pre> 
<?php

$contacts = query('SELECT * FROM members');
while($contact = mysql_fetch_assoc($contacts)){
print('<div style="float:left; border:thin solid #cccccc; width:25px; clear:both;">'.$contact['id'].'</div> 
<div style="float:left; border:thin solid #cccccc; width:50px;">'.$contact['fname'].'</div>
<div style="float:left; border:thin solid #cccccc; width:100px;">'.$contact['lname'].'</div> 
<div style="float:left; border:thin solid #cccccc; width:200px;">'.$contact['address'].'</div>
<div style="float:left; border:thin solid #cccccc;width:200px;">'.$contact['city'].'</div>
<div style="float:left; border:thin solid #cccccc;width:30px;">'.$contact['state'].'</div>
<div style="float:left; border:thin solid #cccccc;width:50px;">'.$contact['zip'].'</div>
<div style="float:left;border:thin solid #cccccc;">
<a href="index.php?action=delete&id='.$contact['id'].'">Delete</a> -  
<a href="index.php?action=edit&id='.$contact['id'].'">Edit</a></div>');
}


	print('<div style="clear:both"></div><br/><br/>'.$mysql_query_error);
?>
<br/><br/>



</body>
</html>

The Notices are in the top of the first block:

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$postId = $_POST['id'];

What makes these variables an undefined index?

Can I also create a variable that I can use for my table ($table) that will be a global variable?

Link to comment
https://forums.phpfreaks.com/topic/149027-solved-update-help/#findComment-782741
Share on other sites

Hi jmr3460,

 

You shouldn't have any errors showing up.  You can fix the undefined indexes by checking to see that the $_POST data has actually been set before you assign it to your variables. 

 

$fname = isset($_POST['fname']) ? $_POST['fname'] : "";
$lname = isset($_POST['lname']) ? $_POST['lname'] : "";
$address = isset($_POST['address']) ? $_POST['address'] : "";

 

and repeat for the rest of them..

 

The code is checking to see if the variable has been set (http://nz2.php.net/isset).  If it has then it assigns the $_POST data to the variable.  If it hasn't been set it will assign it an empty string.  This will fix your undefined index errors.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/149027-solved-update-help/#findComment-782777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.