Page 1 of 1

Updating mysql database.

Posted: Wed Feb 11, 2009 1:21 pm
by fionaom87
Hello, i am trying to update my database but doesnt seem to be working.i want to be able to insert a specific customer id, update its info.

Thanks
F. :D

Code: Select all

 
<!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">
 
<html>
<head>
<title>Update</title>
</head>
<body bgcolor="white">
<form method="POST" action="update1.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Customer ID:</font></td>
<td><input type="text" name="customerid" size=10></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
 

Code: Select all

 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?> 
 
<?php 
 
$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = '.$id;"); 
 
$amount_of_rows = mysql_num_rows($sqlstr); ?> 
 
 
<?php 
if ($amount_of_rows > 0); 
{ 
while ($row = mysql_fetch_array($sqlstr)) { 
?> 
 
<html>
<head>
<title>Update</title>
</head>
<body bgcolor="white">
<form method="post" action="update2.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Customer Name:</font></td>
<td><input type="text" name="name" 
value="<?php echo $row["name"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Customer Address:</font></td>
<td><input type="text" name="address" 
value="<?php echo $row["address"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Telephone number:</font></td>
<td><input type="text" name="telephoneno" 
value="<?php echo $row["telephoneno"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">House Type:</font></td>
<td><input type="text" name="housetype" 
value="<?php echo $row["housetype"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Date:</font></td>
<td><input type="text" name="date" 
value="<?php echo $row["date"]; ?>" size=100></td>
</tr>
 
<tr>
<td><font color="blue">Customer ID:</font></td>
<td><input type="text" name="customerid" 
value="<?php echo $row["customerid"]; ?>" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
 
<?php 
} 
} 
?>
</body>
</html>
 

Code: Select all

 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
 
<html>
<head>
<title>Update 2</title>
</head>
<body bgcolor="white">
<?php include 'config.php'; ?> 
 
<?php
 
 
$name=$_POST['name']; 
$address=$_POST['address']; 
$telephoneno=$_POST['telephoneno']; 
$housetype=$_POST['housetype']; 
$phone=$_POST['phone']; 
$customerid=$_POST['customerid']; 
 
 
$query="UPDATE customers SET name ='$name', address='$address', telephoneno='$telephoneno', housetype ='$housetype', phone ='$phone' WHERE customerid ='$customerid' "; 
mysql_query($query); 
mysql_close($con);
 
?>
</body>
</html>
 

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 2:05 pm
by mbdigital
Hi,

Are you able to input data into the database to begin with? i.e. is it updating or inserting new entries you're having the trouble with?
I would suggest starting by echoing the sql string to establish just what you're asking the mysql to do. from the looks of your string it might not be picking the php variables correctly.

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 2:15 pm
by fionaom87
yes i am able to insert and select from my db, I have only started to learn php and mysql in the last month. Could you elaborate about php variables?
Thansk
F.

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 2:19 pm
by fionaom87
I tried the echo function.following appears in Update1.php

HELLO
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\update1.php on line 10
HELLO
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\update1.php on line 18

Code: Select all

 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?> 
<?php
 echo "HELLO"
 ?>
<?php 
 
$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = '.$id;"); 
 
$amount_of_rows = mysql_num_rows($sqlstr); ?> 
 
<?php
 echo "HELLO"
 ?>
<?php 
if ($amount_of_rows > 0); 
{ 
while ($row = mysql_fetch_array($sqlstr)) { 
?> 
 <?php
 echo "HELLO"
 ?>
 
 

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 3:15 pm
by mbdigital
I would try removing the semi colon from the end of your if statement. Also use the echo comman to echo the mysql query. I.e. echo $sqlstr;

This should give you a clear idea of what you're trying to tell the database to do.

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 3:25 pm
by watson516

Code: Select all

$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = '.$id;");
That line is messed up.

try something like

Code: Select all

$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = $id");
Your errors are telling you that your sql statement is wrong.

edit

You can't mix " and '
If you are concatenating a string and a variable, you dont need that extra " at the end
A ; is used at the end of statements only

Re: Updating mysql database.

Posted: Wed Feb 11, 2009 3:35 pm
by fionaom87
I am now getting this error
Parse error: parse error in C:\wamp\www\update1.php on line 67 :banghead: :banghead:

Code: Select all

 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?> 
<?php
 echo "HELLO"
 ?>
<?php 
 
$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = '.$customerid;"); 
 
$amount_of_rows = mysql_num_rows($sqlstr); ?> 
 
<?php
 echo "HELLO"
 ?>
<?php 
if ($amount_of_rows > 0)
{ 
while ($row = mysql_fetch_array($sqlstr)) { 
?> 
 <?php
 echo "HELLO"
 ?>
 
 
 
<html>
<head>
<title>Update</title>
</head>
<body bgcolor="white">
<form method="post" action="update2.php">
<table>
<col span="1" align="right">
<tr>
 
 
<td><font color="blue">Customer Name:</font></td>
<td><input type="text" name="name" 
value="<?php echo $row["name"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Customer Address:</font></td>
<td><input type="text" name="address" 
value="<?php echo $row["address"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Telephone number:</font></td>
<td><input type="text" name="telephoneno" 
value="<?php echo $row["telephoneno"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">House Type:</font></td>
<td><input type="text" name="housetype" 
value="<?php echo $row["housetype"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Date:</font></td>
<td><input type="text" name="date" 
value="<?php echo $row["date"]; ?>" size=100></td>
</tr>
 
<tr>
<td><font color="blue">Customer ID:</font></td>
<td><input type="text" name="customerid" 
value="<?php echo $row["customerid"]; ?>" size=100></td>
</tr>
 
<td><input type="submit" value="Submit"></td>
 
 
 
</body>
</html>