Jump to content

Update specific fields in SQL using PHP


nic0

Recommended Posts

Hi all, i'm very new to this and would greatly appreciate any help.

 

I need help on a form which a user can enter a specific customers name and then update a specific field based on their name.

 

i.e. if I enter the customer name Bob into a text box and enter his age in another text box I need the PHP script to look for Bob then update this age.

 

First Page shows a table from the database so the user can see whos in it:

 

<?php
$con = mysql_connect("***","***","**");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$result = mysql_query("SELECT * FROM Customer");

echo "<form name='update' action='606.php' method='post'>
<h2>Update Order Status</h2><br><table border='1'>
<p>Current Customer Status</p>
<tr>
<th>FirstName</th>
<th>Order Stage</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['Stage'] . "</td>";
  echo "</tr>";
  }
echo "</table><br><p>Customer Name<p><input type='text' name='n1'><br><p>Order Stage<p><input type='text' name='n2'><br><input type='submit' name='submit' value='Update' />";

mysql_close($con);
?> 

 

Which then runs 606.php

 

<?php
$con = mysql_connect("***","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

//what goes here?
WHERE FirstName = $_POST['n1.TEXT']");

mysql_close($con);
?> 

 

So in this example i would like to enter Bob into the text box n1, then from this update his order stage from text box n2.

 

Can anyone help?

Link to comment
Share on other sites

Hi

 

You would use something like:-

 

<?php
$con = mysql_connect("***","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

//what goes here?
UPDATE sometable SET order_stage = '".$_POST['n2']."' WHERE FirstName = '".$_POST['n1']."'";

mysql_close($con);
?>  

 

However you should validate the input first (and use mysql_real_escape_string to avoid problems with people putting quotes and dodgy sql in the input fields).

 

All the best

 

Keith

Link to comment
Share on other sites

i.e. if I enter the customer name Bob into a text box and enter his age in another text box I need the PHP script to look for Bob then update this age.

 

I would not use this approach as what if your customer's name is Alidimagit or some other name you might find hard writing. Instead display all users in a table add a button to edit the user's age which passes the user's id

Link to comment
Share on other sites

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.