Jump to content

Recommended Posts

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. :(

 

Link to comment
https://forums.phpfreaks.com/topic/102686-solved-problem-with-update/
Share on other sites

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

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.

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";
 ?>

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. ???


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'] . "'");






This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.