Simple php problem

Hi
any one tell me what’s wrong in this coding?

i want to write update command in php. if anyone know thne please reply me i am waiting…

<?php
if(isset($_POST[‘Update’]))
{

$sql=“SELECT * FROM client WHERE id=‘1’”;
$result= mysql_query($sql) or die(mysql_error());

if($row=mysql_fetch_row($result))
{

While($row)

$r="UPDATE client SET age='$age'";

}
}

?>

thanks

  1. You do not ever execute the UPDATE query. You just defined a string, $r, containing some text. You have to call mysql_query() to send that text to the MySQL server.

  2. Your query is inside an infinite loop, and does not in any way use the data you retrieved from the previous SELECT query. If you did call mysql_query() within the while loop, it would send that query to the server as many times as possible before the script execution time limit is reached. The query would also update all rows of the table with the new value, since you specify no WHERE clause restricting it to whatever row you wanted to update.

I think you want something more like this:

if (isset($_POST['update'])) {

  $query = "UPDATE client SET age = " . (int)$_POST['age'] . " WHERE id = " . (int)$_POST['id'];
  mysql_query($query);

}

Note that numeric values should not be enclosed in single quotes in SQL.

your code is

<?php
if(isset($_POST[‘Update’]))
{
$sql=“SELECT * FROM client WHERE id=‘1’”;
$result= mysql_query($sql) or die(mysql_error());
if($row=mysql_fetch_row($result))
{

While($row)

$r=“UPDATE client SET age=‘$age’”;

}
}

?>

in updation there is no need of select query you should write only
<?php
if(isset($_POST[‘Update’]))
{
mysql_connect(‘localhost’, ‘root’, ‘password’) or die(“cannot connect the database”);
mysql_select_db(“databasename”) or die(“can not open database”);
$age = $_POST[‘age’];
$r = “UPDATE client SET age=‘$age’ WHERE id=‘1’”;
$res = mysql_query($r);
}

?>

Hi

thanks for your reply

but i want to update the field via form
Please check this

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> Update </TITLE>

</HEAD>

<BODY>

<?php

include(‘configure.php’);
if(isset($_POST[‘Update’]))
{

$id=$_GET[‘id’];

$sql = "SELECT * FROM client WHERE `id` = " . mysql_real_escape_string ( $_GET['id'] );


if ($query = mysql_query ( $sql ) )
{

	$row = mysql_fetch_assoc ( $query );

	
		if ( $_POST [ 'name' ] != '' && $_POST [ 'email' ] != '' )
		{
		

			$query = 	"UPDATE 
						client 
					SET
						`name` = " . mysql_real_escape_string ( $_POST [ 'name' ] ) . "
						`email` = " . mysql_real_escape_string ( $_POST [ 'email' ] ) . "
					WHERE
						`id` = " . mysql_real_escape_string ( $_GET['id'] );

			if (mysql_query ( $query ) )
			{
				$success = 'Query updated successfully!';
			}
			else {
				die ( mysql_error () );
			}
		}
		else {
			echo 'Please ensure that you have a title and some content for this article!';
		}
	
}
else {
	die ( mysql_error () );
}

}
if ( isset ( $success ) ) {
echo $success;
}
else {
//we need this form only if we don’t have the success message
?>
<form id=“update” name=“update” method=“post” action=“<?=$_SERVER[‘PHP_SELF’]?>”>

	Name:&lt;br /&gt;
	&lt;input name="name" type="text" id="name" size="55" value="&lt;?php if ( isset ( $_POST['name'] ) )

{ echo $_POST[‘name’]; }

else{ echo $row[‘name’];}
?>" />
<br /><br />
Email:<br />
<input name=“email” cols=“55” rows=“5” id=“email”>

	&lt;?php if ( isset ( $_POST['email'] ) )

	{ echo $_POST['email']; }

else{ echo $row[‘email’];} ?>
<br /><br />
<input type=“update” name=“Update” value=“update” />
</form>
<?php } ?>

</BODY>
</HTML>

You need single quotes around the non-numeric values in your UPDATE query

Hi

i created this query Please check this

when i executed this query this display this

Notice: Undefined index: id in C:\wamp\www\u.php on line 19

<?php

include(‘configure.php’);

$id=$_POST[‘id’];
$name=“name”;
$email=“email”;

$query=“SELECT * FROM client WHERE id=‘$id’”;
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;

while ($i < $num) {

$name=mysql_result($result,$i,“name”);

$email=mysql_result($result,$i,“email”);

++$i;

}
?>

<form action=“updated.php” method=“post”>

<input type=“hidden” name=“id” value=“<? echo $id; ?>”>
Name: <input type=“text” name=“name” value=“<? echo $name; ?>”><br>

E-mail <input type=“text” name=“email” value=“<? echo $email; ?>”><br>
<input type=“Submit” value=“Update”>

</form>

and updated.php

<?php

include(‘configure.php’);
$id=$_POST[‘id’];
$name=$_POST[‘name’];
$email=$_POST[‘email’];

$query=“UPDATE client SET name=‘$name’,email=‘$email’ WHERE id=‘$id’”;
mysql_query($query);
echo “Record Updated”;
?>

but problem is record is not updated.

please reply me.

thanks

The reason it is giving you that error is because the form that is passing the POST data to it, is not passing a value for “id”

hi

with the help of this loop i am getting all values from the table but i want to show only single value so please tel me how i do this

while($row=mysql_fetch_array($result))
{
?>
Name: <? echo $row[1]; ?> <br>
E-mail: <? echo $row[1]; ?> <br>

<?
}
?>

Are you expecting a single row? Or expecting multiple and wanting to show the first?

hi

i have lots of records in my database if i use above coding then all the records displayed
but i want to show only one record in one time.

reply me

thanks

Easier if you answer the question. If you select multiple records, then the loop will display them. If you only want one record, then the query should reflect that and the loop will only display one.

Your query a few posts above selects by id, which should be unique, hence no possibility of getting multiple records.