theloki Posted August 10, 2010 Share Posted August 10, 2010 this is what i have so far but i cant get it to update the data update page <?php $host="localhost"; // Host name $username="**"; // Mysql username $password="*****"; // Mysql password $db_name="manews"; // Database name $tbl_name="Shows"; // 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 in mysql</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>Day</strong></td> <td align="center"><strong>Month</strong></td> <td align="center"><strong>Year</strong></td> <td align="center"><strong>Song</strong></td> <td align="center"><strong>Data</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="Day" type="text" id="Day" value="<? echo $rows['Day']; ?>"></td> <td align="center"><input name="Month" type="text" id="Month" value="<? echo $rows['Month']; ?>"></td> <td align="center"><input name="Year" type="text" id="Year" value="<? echo $rows['Year']; ?>" size="15"></td> <td align="center"><input name="Song" type="text" id="Song" value="<? echo $rows['Song']; ?>"></td> <td align="center"><input name="Data" type="text" id="Data" value="<? echo $rows['Data']; ?>" 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 script <?php $host="localhost"; // Host name $username="**"; // Mysql username $password="**"; // Mysql password $db_name="manews"; // Database name $tbl_name="Shows"; // 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"); // update data in mysql database $sql="UPDATE $tbl_name SET Day='$Day', Month='$Month', Year='$Year', Year='$Song', Year='$Data' WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { echo "ERROR"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/ Share on other sites More sharing options...
litebearer Posted August 10, 2010 Share Posted August 10, 2010 where is the code that retrieves the form data? Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097453 Share on other sites More sharing options...
theloki Posted August 10, 2010 Author Share Posted August 10, 2010 here it is <?php $host="localhost"; // Host name $username="***"; // Mysql username $password="****"; // Mysql password $db_name="manews"; // Database name $tbl_name="Shows"; // 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>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Day</strong></td> <td align="center"><strong>Month</strong></td> <td align="center"><strong>Year</strong></td> <td align="center"><strong>Song</strong></td> <td align="center"><strong>Data</strong></td> <td align="center"><strong>Update</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['Day']; ?></td> <td><? echo $rows['Month']; ?></td> <td><? echo $rows['Year']; ?></td> <td><? echo $rows['Song']; ?></td> <td><? echo $rows['Data']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097456 Share on other sites More sharing options...
RON_ron Posted August 10, 2010 Share Posted August 10, 2010 try this <?php $link = mysql_connect("localhost","id","thepw"); mysql_select_db("dbname"); $query = "INSERT INTO db_name (field1, field2, field3, field4) VALUES('$data1','$data2','$data3','$data4')"; $result = mysql_query($query); $sentOk = "The data has been added to the database."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097458 Share on other sites More sharing options...
theloki Posted August 10, 2010 Author Share Posted August 10, 2010 were would i add that into what file Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097478 Share on other sites More sharing options...
xcoderx Posted August 10, 2010 Share Posted August 10, 2010 any error message? Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097504 Share on other sites More sharing options...
theloki Posted August 10, 2010 Author Share Posted August 10, 2010 no it just says it has done it Successful but the data hasnt updated Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097521 Share on other sites More sharing options...
Ruzzas Posted August 10, 2010 Share Posted August 10, 2010 If you guys noticed, He's trying to update not insert... Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097529 Share on other sites More sharing options...
theloki Posted August 10, 2010 Author Share Posted August 10, 2010 can anyone help me with this for a update not insert as has been put to help me from others Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097626 Share on other sites More sharing options...
DavidAM Posted August 10, 2010 Share Posted August 10, 2010 // update data in mysql database $sql="UPDATE $tbl_name SET Day='$Day', Month='$Month', Year='$Year', Year='$Song', Year='$Data' WHERE id='$id'"; $result=mysql_query($sql); You are referencing variables that have not been defined in your script. Specifically $Day, $Month, $Year, $Song, $Data and $id. It would appear that you expect these to be coming from the form. However, the register_globals functionality of PHP has been turned off and will be removed from a soon-to-be-released version of PHP. It was a major security whole. You need to retrieve these variables from the $_POST array before referencing them. You should also sanitize them to prevent SQL injections. And check that they are valid $day = mysql_real_escape_string($_POST['day']); // etc. etc., etc., // OR if they are integers, you can use $day = intval($_POST['day']); // which might end up as zero if it was not a valid number Note: event the "id" value which you pass as a hidden field needs to be sanitized and validated. Quote Link to comment https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/#findComment-1097798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.