Jump to content

Update MySQL through PHP


johnbruce

Recommended Posts

Hi all, I've currently got a script which allows a user to search for a record in a mysql database and then returns the record into a html text field.

 

What I'm wanting to do is allow the user to edit this record and submit it back to the database and update the record. The code i'm currently using is;

 

<?
$ud_id=$_GET['id'];
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);

$query="UPDATE gosscustomers SET title='$ud_title',first_name='$ud_first_name', surname='$ud_surname', company='$ud_company', address_1='$ud_address_1', address_2='$ud_address_2', address_3='$ud_address_3', postcode='$ud_postcode', country='$ud_country', email_address='$ud_email_address' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

?> 

 

The code from the page that shows the search results is here:

 

<html>
<style type="text/css">
<!--
.style1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 10px;
}
-->
</style>
<?
$username="";
$password="";
$database="";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM gosscustomers WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Search Results</center></b><br><br>";

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"title");
$first_name=mysql_result($result,$i,"first_name");
$surname=mysql_result($result,$i,"surname");
$company=mysql_result($result,$i,"company");
$address_1=mysql_result($result,$i,"address_1");
$address_2=mysql_result($result,$i,"address_2");
$address_3=mysql_result($result,$i,"address_3");
$postcode=mysql_result($result,$i,"postcode");
$country=mysql_result($result,$i,"country");
$email_address=mysql_result($result,$i,"email_address");
?>  
<span class="style1"><br />
</span>
<form id="form1" name="form1" method="post" action="update.php">
  <span class="style1">Record ID:<br />
  <input name="ud_id" type="text" id="ud_id" value="<?php echo $id; ?>" size="40" />
  <br />
  Title: <br />
  <input name="ud_title" type="text" id="ud_title" value="<?php echo $title; ?>" size="40" />
<br />
First name: <br />
<input name="ud_first_name" type="text" id="ud_first_name" value="<?php echo $first_name; ?>" size="40" />
<br />
Surname: <br />
<input name="ud_surname" type="text" id="ud_surname" value="<?php echo $surname; ?>" size="40" />
<br />
Company: <br />
<input name="ud_company" type="text" id="ud_company" value="<?php echo $company; ?>" size="40" />
<br />
Address Line 1:<br />
<input name="ud_address_1" type="text" id="ud_address_1" value="<?php echo $address_1; ?>" size="40" />
<br />
Address Line 2: <br />
<input name="ud_address_2" type="text" id="ud_address_2" value="<?php echo $address_2; ?>" size="40" />
<br />
Address Line 3: <br />
<input name="ud_address_3" type="text" id="ud_address_3" value="<?php echo $address_3; ?>" size="40" />
<br />
Postcode: <br />
<input name="ud_postcode" type="text" id="ud_postcode" value="<?php echo $postcode; ?>" size="40" />
<br />
Country: <br />
<input name="ud_country" type="text" id="ud_country" value="<?php echo $country; ?>" size="40" />
<br />
E-Mail address:<br />
<input name="ud_email_address" type="text" id="ud_email_address" value="<?php echo $email_address; ?>" size="40" />
  </span>
  <br />
  <br />
  <input type="submit" name="Submit" value="Update" />
</form>
<span class="style1"><br />
<br />
<br />
<?
$i++;
}
?>
</span>
</html>

 

Any help would be greatly appreciated!

 

Thanks,

Link to comment
Share on other sites

Can acyou establish a connection?

 

if not you may want to use your queries like this:

 

<?php
$connection=mysql_connect(localhost,$username,$password);
$query="UPDATE gosscustomers SET title='$ud_title' WHERE id='$ud_id'";
mysql_query($query,$connection);
?>

Link to comment
Share on other sites

You are just using variables without giving them values.

some thing like:

<?php
$ud_title = $_POST['ud_title'];
?>

 

in here

 

<?php
$ud_id=$_GET['id'];
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);

$query="UPDATE gosscustomers SET title='$ud_title',first_name='$ud_first_name', surname='$ud_surname', company='$ud_company', address_1='$ud_address_1', address_2='$ud_address_2', address_3='$ud_address_3', postcode='$ud_postcode', country='$ud_country', email_address='$ud_email_address' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

?> 

Link to comment
Share on other sites

Remember when you used this:

<?php
$ud_id=$_GET['id'];
?>

 

First of all,

You should have used $_POST instead of $_GET

$_GET is when you have something like this: mypage.php?id=3

 

so for every input in your form you should pass it's value to a variable

 

example:

<input name="id" value="3">

$id = $_POST["id"]

example:

<input name="mytext" >

$mytext = $_POST["mytext"]

 

You would like to get all the data which you just sent.

 

Link to comment
Share on other sites

Grrr! Still nothing happening, I'm also still not gettting any error messages! I've attached the two files, if anyone could take a look and let me know why it's not writing back to the database i'd be very gratefull.

 

 

[attachment deleted by admin]

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.