PHP and updating mysql database.
Posted by: fionaom87 (194.0.77.---)
Date: February 09, 2009 10:20PM

Hello, the following code doesnt seem to be working. I just want to update records in mysql db.

<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?>
<?php

$formVars[$varname]=$value;

$query="SELECT * FROM customers WHERE customerid = \"".$formVars["customerid"]."\"";
echo $query;
$result=mysql_query($query) or die(mysql_error());

$row=mysql_fetch_array($result);
$formVars = array();
$formVars["name"]=$row["name"];
$formVars["address"]=$row["address"];
$formVars["telephoneno"]=$row["telephoneno"];
$formVars["housetype"]=$row["housetype"];
$formVars["customerid"]=$row["customerid"];
mysql_close($con);
?>

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: February 09, 2009 10:24PM

u want to update? u have no update line in the code?

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: fionaom87 (194.0.77.---)
Date: February 09, 2009 10:28PM

Apologies i want to select it first, then update. My next page is

<?php error_reporting (E_ALL ^ E_NOTICE); ?>

<html>
<head>
<title>Update 2</title>
</head>
<body bgcolor="white">
<?php include 'config.php'; ?>

<?php
$formVars[$varname]=$value;

echo "Record updated<br><a href=\"update.html\">click here</a> to update another record<br>";
$query="UPDATE customers set ".
"name= \"".$formVars["name"]."\",".
"address= \"".$formVars["address"]."\",".
"telephoneno= \"".$formVars["telephoneno"]."\",".
"housetype= \"".$formVars["housetype"]."\",".
"phone= \"".$formVars["phone"]."\",".

"\" WHERE customerid = \"".$formVars["customerid"]."\"";
mysql_query($query);
mysql_close($con);
?>
</body>
</html>

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: February 09, 2009 10:33PM

fiona can i ask where ur getting the code from .. a book? or online /

it seems to be an extremly complex way or writing php

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: fionaom87 (194.0.77.---)
Date: February 09, 2009 10:35PM

yes it is from a book. Alot of stuff i get from books from college and manipulate to my own.

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: February 09, 2009 10:52PM

$query="UPDATE customers set ".
"name= \"".$formVars["name"]."\",".
"address= \"".$formVars["address"]."\",".
"telephoneno= \"".$formVars["telephoneno"]."\",".
"housetype= \"".$formVars["housetype"]."\",".
"phone= \"".$formVars["phone"]."\",".

"\" WHERE customerid = \"".$formVars["customerid"]."\"";



this would be usualy written


$name=$formVars['name'];
$address=$formVars['address'];
$telephoneno=$formVars['telephoneno'];
$housetype=$formVars['housetype'];
$phone=$formVars['phone'];
$customerid=$formVars['customerid'];


$query="UPDATE customers SET name ='$name', address='$address', telephoneno='$telephoneno', housetype ='$housetype', phone ='$phone' WHERE customerid ='$customerid' ";
mysql_query($query);
mysql_close($con);

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]



Edited 1 time(s). Last edit at 02/09/2009 10:53PM by stevenmartin99.

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: fionaom87 (194.0.77.---)
Date: February 09, 2009 11:08PM

I have it now selecting from the db, but i want the person to be able to input a customer id number e.g. 7 and the following page have customer 7's details appear and you can update them.

//update
<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>





//update 1


<?php

$sqlstr = mysql_query("SELECT * FROM customers WHERE customerid = '.$id;"winking smiley;

$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
}
}
?>





//update 2

<?php include 'config.php'; ?>

<?php

$formVars[$varname]=$value;
$name=$formVars['name'];
$address=$formVars['address'];
$telephoneno=$formVars['telephoneno'];
$housetype=$formVars['housetype'];
$phone=$formVars['phone'];
$customerid=$formVars['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>

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: February 09, 2009 11:12PM

is ur config.php getting the post of formVars?

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: fionaom87 (194.0.77.---)
Date: February 09, 2009 11:15PM

this is my config.php
<?php
$host = "localhost"; //database location
$user = "root"; //database username
$pass = ""; //database password
$db_name = "test"; //database name

//database connection
$con = mysql_connect("localhost", "root", ""winking smiley or die(mysql_error());
mysql_select_db("test", $con) or die(mysql_error());


//sets encoding to utf8
mysql_query("SET NAMES utf8"winking smiley;
?>

should i change formVars to $con?

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: February 09, 2009 11:22PM

no but u need to get the values from update1 posted to upadate2

$formVars[$varname]=$value;
$name=$formVars['name'];
$address=$formVars['address'];
$telephoneno=$formVars['telephoneno'];
$housetype=$formVars['housetype'];
$phone=$formVars['phone'];
$customerid=$formVars['customerid'];

this doesnt do that
i think ur getting confused with arrays. what is

$formVars[$varname]=$value;

what is formvars?



Ud need
$name=$_POST['name'];

etc etc

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: PHP and updating mysql database.
Posted by: yfastud (Moderator)
Date: February 09, 2009 11:50PM

fiona and steven
this forums are for wampserver and related topics, but not for coding, so suggest you guys communicate through pm or email
thanks

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote


Sorry, only registered users may post in this forum.