Jump to content

overwrite data stored on a DB


radiohomer

Recommended Posts

hey guys

 

i have this code

 

FILE NAME "index.php"

<?php

// make a connection to the database
mysql_connect ("localhost", "root", "vertrigo") or die ('Error: I Failed To Connect To The Database ' . mysql_error());
mysql_select_db ("test");

// Get Data
$query = mysql_query("SELECT * FROM TestTable");

// display the data and loop
while ($row = mysql_fetch_array($query)) {
	echo "<br /> ID: ".$row['ID']."<br /> First Name: ".$row['FName']."<br /> Last Name: ".$row['LName']."<br /> Contact Number: ".$row['CNumber']."<br />";}


?>

<form method="post" action="update.php">
<table border="1" align="center">
  <tr>
    <td align="right" width="220">ID: </td>
    <td align="left" width="220"> <input type="text" name="ID" size="30" /></td>
  </tr>
  <tr>
    <td align="right" width="220">First Name: </td>
    <td align="left" width="220"> <input type="text" name="FName" size="30" /></td>
  </tr>
  <tr>
    <td align="right" width="220">Last Name: </td>
    <td align="left" width="220"> <input type="text" name="LName" size="30" /></td>
  </tr>
  <tr>
    <td align="right" width="220">Contact Number: </td>
    <td align="left" width="220"> <input type="text" name="CNumber" size="30" /></td>
  </tr>
  <tr>
    <td align="right" width="220"><input type="reset" value="Reset" /> </td>
    <td align="left" width="220"> <input type="submit" value="Update Database" /></td>
  </tr>
</table>
</form>

 

and i also have this code

 

FILE NAME "update.php"

<?php
$ID = $_POST['ID'];
$FName = $_POST['FName'];
$LName = $_POST['LName'];
$CNumber = $_POST['CNumber'];

mysql_connect ("localhost", "root", "vertrigo") or die ('Error: I Failed To Connect To The Database ' . mysql_error());
mysql_select_db ("test");

$query="INSERT INTO testtable (ID, FName, LName, CNumber)VALUES ('".$ID."','".$FName."', '".$LName."', '".$CNumber."')";

mysql_query($query) or die ('Error Updating Database');

echo "Database Updated Sucsessfully With: ".$ID." ".$FName." ".$LName." ".$CNumber ;

?>

 

ok so the script is working like a charm

its sending the data to the database as i want it to.

the problem i have is that i want to be able to update the info that is already on the database

 

lets say i want to change a phone/contact number

i have typed the ID number into the ID text field and the same first and last name into there correct boxes and then typed in the new phone number

i then click submit and i get the error ""Error Updating Database""

i have looked all over the forum and net to see what i have done wrong to not allow this code to update

 

can anyone help me out here please

im quite new to the php language and could really do with some pointers

 

thanks

Steve

Link to comment
Share on other sites

If you want to update something you will have to use UPDATE in the mysql string. Example±

 

mysql_query("UPDATE `users` SET `option1`='value', `option2`='value2' WHERE `condition`='something'");

 

So, add an if where you check if there is already an entry in the database, if it is; update otherwise insert.

Link to comment
Share on other sites

so how would i place that code onto the file i have

again i dont know much about php and im still learning

 

would i just put that code into the update.php or do i need to change / add things to it?

 

it would be very helpful if you could insert the values to check "FName" "LName" "CNumber"

or maybe a guide or something i can follow

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.