Jump to content

Really need help on php form to update 3 mysql values


Mikesag

Recommended Posts

 
Been racking my brains for 11 hours now trying to figure out why this script is not updating my 3 values to mysql
 

 


// listrecords.php
''****************************************************************


<?php 
$hostname="private"; // Host name 
$username="dbmikesecure"; // Mysql username 
$password="private"; // Mysql password 
$database="dbmikesecure"; // Database name 
$tbl_name="Data"; // Table name


// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


$sql="SELECT * FROM $tbl_name"; 
$result=mysql_query($sql); 
?> 
<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td> 
<table width="400" border="1" cellspacing="0" cellpadding="3"> 
<tr> 
<td colspan="4"><strong>Data List </strong> </td> 
</tr> 
<tr> 
<td align="center"><strong>Word1</strong></td> 
<td align="center"><strong>Word2</strong></td> 
<td align="center"><strong>Word3</strong></td> 
<td align="center"><strong>Update</strong></td> 
</tr> 
<?php 
while($rows=mysql_fetch_array($result)){ 
?> 
<tr> 
<td><? echo $rows['word1']; ?></td> 
<td><? echo $rows['word2']; ?></td> 
<td><? echo $rows['word3']; ?></td> 


// link to update.php and send value of id 
<td align="center"><a href="update.php?Id=<? echo $rows['Id']; ?>">update</a></td> 
</tr> 
<?php 
} 
?> 
</table> 
</td> 
</tr> 
</table> 
<?php 
mysql_close(); 
?> 






// update.php
''****************************************************************


<?php
$hostname="private"; // Host name 
$username="dbmikesecure"; // Mysql username 
$password="private"; // Mysql password 
$database="dbmikesecure"; // Database name 
$tbl_name="Data"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


// get value of id that sent from address bar
$id=$_GET['id'];




// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);


$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update Data</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Word1</strong></td>
<td align="center"><strong>Word2</strong></td>
<td align="center"><strong>Word3</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="word1" type="text" id="word1" value="<? echo $rows['word1']; ?>"></td>
<td align="center"><input name="word2" type="text" id="word2" value="<? echo $rows['word2']; ?>" size="15"></td>
<td><input name="word3" type="text" id="word3" value="<? echo $rows['word3']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="Id" type="hidden" id="Id" value="<? echo $rows['Id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>


<?


// close connection 
mysql_close();


?>






// update_ac.php
''****************************************************************


<?php
$hostname="private"; // Host name 
$username="dbmikesecure"; // Mysql username 
$password="private"; // Mysql password 
$database="dbmikesecure"; // Database name 
$tbl_name="Data"; // Table name 




// Connect to the database 
mysql_connect($hostname, $username, $password) OR DIE("Unable to connect"); 
mysql_select_db("$database") or die("Unable to select database");


$query="UPDATE Date SET word1='" . $_POST['word1'] . "', word2='" . $_POST['word2'] . "', word3='" . $_POST['word3'] . "' WHERE id='" . $_POST['$Id'] . "'"; 
$checkresult = mysql_query($query); 
if ($checkresult) echo 'update query succeeded'; 
else echo 'update query failed';


mysql_close(); 
?> 
 
Edited by fenway
code tags
Link to comment
Share on other sites

1. Please use code tags so we can read the code better.

2. You're not checking for mysql errors. See my signature.

3. Don't use user input without sanitizing it.

4. Don't put quotes around variables in PHP, or numbers in MySQL

5. Date is a reserved word, you should pick a different name for your table or use ` around it.

Edited by Jessica
Link to comment
Share on other sites

if i change the query to this

 

 

 

$query="UPDATE `Data` SET `word1`='$word1' WHERE `id`=1";
 

in update_ac.php

 

then its does not update the value

 

but if i put this without the $ sign, at least it places the actual word,, (word1), lol,, in the database,,

 

 

$query="UPDATE `Data` SET `word1`='word1' WHERE `id`=1";
 

 

so i think the problem is that the update.php page is not sending the update_ac.php page the value ?

 

can you check this for me,, ive been trying to send the value on my side but cant seem to do it,,

Edited by Mikesag
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.