mjgdunne Posted April 24, 2008 Share Posted April 24, 2008 Hi i need to be able to update records in my mysql database, i have a search fuction which allows the admin user to update the fields these updates should be saved to the database. I am having trouble as i do not know how i would use the WHERE in the query. I am taking the information saved in a session as an array. The details are: "myfirstname" "mysurname" "myusername" "mypassword" "myemail" "mybrowser" I need to be able to update all fields. Here is my code: <?php session_start(); ?> <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Details Updated</H1> <form action="login_success3.php" method="post"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <TD ALIGN=CENTER VALIGN=TOP WIDTH=50> </TD> <TD ALIGN=LEFT VALIGN=TOP WIDTH=83%> <TR><TD> <?php $_SESSION['result'] = "$result"; ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $row = $_SESSION['members'][0]; $myfirstname=$_POST['myfirstname']; $mysurname=$_POST['mysurname']; $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $myemail=$_POST['myemail']; $mybrowser=$_POST['mybrowser']; mysql_select_db("test"); $query="update members SET Firstname='" . $_POST['myfirstname'] . "', Surname='" . $_POST['mysurname'] . "', username='" . $_POST['myusername'] . "', password='" . $_POST['mypassword'] . "', email='" . $_POST['myemail'] . "', level='" . $_POST['mybrowser'] ."'"; echo "Updated"; ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <td align="center"><input type="submit" value="Home"/></td> </tr> </table> </table> </td> </form> </tr> </table> </BODY> </HTML> Any help would be great thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/ Share on other sites More sharing options...
knowj Posted April 24, 2008 Share Posted April 24, 2008 All tables must have a primary key field that is an auto increment INT (well some for of int whether tinyint etc...). You would usually contain the id within the url for instance <a href="myeditscript.php?id=1">post 1</a> at the end of the SQL script you would then have "yoursql query... WHERE id='$_GET[id]'"; Just remember if your script is open to anyone that isn't trusted (the general public/subscribed members) always protect your database against SQL injection $_GET['id'] = mysql_real_escape_string($_GET['id']); hope this helps J Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525874 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 Hi, yes i have an id field in my database, i have printed it out in the edit results as: echo "<tr><th>ID</th>"; echo "<td>"; echo '<input type="hidden" name="myid" value="'. $row['id'] .'" size="20" />'; echo "</td>"; And i have changed the code below but it does not seem to update the fields, im probably missing something very simply. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525877 Share on other sites More sharing options...
Wolphie Posted April 24, 2008 Share Posted April 24, 2008 Try "UPDATE `db_name` . `tbl_name` SET `field_name` = '$value' WHERE `field_name` = '$value'" Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525879 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 Sorry i forgot to add the code: <?php $_SESSION['result'] = "$result"; ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $row = $_SESSION['members'][0]; $myid=$_POST['myid']; $myfirstname=$_POST['myfirstname']; $mysurname=$_POST['mysurname']; $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $myemail=$_POST['myemail']; $mybrowser=$_POST['mybrowser']; mysql_select_db("test"); $query="update members SET Firstname='" . $_POST['myfirstname'] . "', Surname='" . $_POST['mysurname'] . "', username='" . $_POST['myusername'] . "', password='" . $_POST['mypassword'] . "', email='" . $_POST['myemail'] . "', level='" . $_POST['mybrowser'] . "' WHERE id='" . $_POST['myid'] . "'"; echo "Updated"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525880 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 I ran a echo $query; after the query any it prints off: update members SET Firstname='Michael1', Surname='Dunne', username='mjgdunne', password='rainbow', email='[email protected]', level='1' WHERE id='1'Updated But it still doesnt update the database? This is really bugging me any help would be great. ??? Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525884 Share on other sites More sharing options...
zenag Posted April 24, 2008 Share Posted April 24, 2008 have u forget to write mysql_query....... $query=mysql_query("update members SET Firstname='" . $_POST['myfirstname'] . "', Surname='" . $_POST['mysurname'] . "', username='" . $_POST['myusername'] . "', password='" . $_POST['mypassword'] . "', email='" . $_POST['myemail'] . "', level='" . $_POST['mybrowser'] . "' WHERE id='" . $_POST['myid'] . "'"); Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525898 Share on other sites More sharing options...
mjgdunne Posted April 24, 2008 Author Share Posted April 24, 2008 Thank you, iv been looking at it so long now my eyes are gone square and i missed it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/#findComment-525904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.