illuz1on Posted May 6, 2007 Share Posted May 6, 2007 Hey I have always had a problem with updating rows in SQL, and now this is the best piece of update code I have and it wont work?!? Please help me spot the error here its killing me I send the url like this edit.php?id=$id and this is my code, which just brings up a blank page weather ID is posted or even just edit.php <?php include("db.php"); if($submit) { $title = $_POST['title']; $dur1 = $_POST['dur1']; $description = $_POST['description']; $price = $_POST['price']; $id = $_POST['id']; $result = mysql_query("UPDATE specialoffer SET title='$title', dur1='$dur1', price='$price', description='$description' WHERE id='$id' ",$con); echo "<b>Offer UPDATED Successfully!<br>You'll be redirected to admin page after (4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=admin.php>"; } elseif($id) { $result = mysql_query("SELECT * FROM specialoffer WHERE id='$id' ",$con); while($myrow = mysql_fetch_assoc($result)) { $id = $myrow["id"]; $title = $myrow["title"]; $price = $myrow["price"]; $dur1 = $myrow["dur1"]; $description = $myrow["description"]; ?> <br> <h3>::Edit News</h3> <form method="post" action="<?php echo $PHP_SELF; ?>"> <input type="hidden" name="id" value="<? echo $myrow['id']; ?>"> Title: <input name="title" size="40" maxlength="255" value="<? echo $title; ?>"> <br> Duration: <input name="dur1" size="40" maxlength="255" value="<? echo $dur1; ?>"> <br> Price Range: <input name="price" size="40" maxlength="255" value="<? echo $price; ?>"> <br> Description: <textarea name="description" rows="7" cols="30"><? echo $description; ?></textarea> <br> <input type="submit" name="submit" value="Update Offer"> </form> <? }//end of while loop }//end else ?> Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/ Share on other sites More sharing options...
Psycho Posted May 6, 2007 Share Posted May 6, 2007 Well, I haven't read through all the code, but perhaps you don't hae Register Globals enabled (which you shouldn't anyway). You should be referring to the variables as: $_POST[submit], $_GET[id], and $_SERVER[php_SELF]. Did you even test to see if the page is receiving the variables in question? Also, why are you using a while loop? Are you planning on the id matching more than 1 record? If so, I think you will run into problems with mutiple forms when you are not giving them unique names. Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246851 Share on other sites More sharing options...
makka Posted May 6, 2007 Share Posted May 6, 2007 i think i see your prob when you try and get the id you are using $id = $_POST['id']; when it should be $id = $_GET['id'] p.s try not to use <? use <?php Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246854 Share on other sites More sharing options...
illuz1on Posted May 6, 2007 Author Share Posted May 6, 2007 Thanks guys, ok I tried changing the ID to GET instead of POST and it still has a blank display.. Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246864 Share on other sites More sharing options...
illuz1on Posted May 6, 2007 Author Share Posted May 6, 2007 ok now I tried making a different one, a simpler one without complication/// edit2.php is the form page <? require_once("db.php"); $id = $_GET['id']; $sql = "SELECT * FROM specialoffer WHERE id='$id'"; $data = mysql_query($sql); while($record = mysql_fetch_assoc($data)) { $title = $record['title']; $dur1 = $record['dur1']; $price = $record['price']; $description = $record['description']; } ?> <form action="edit1.php" method="post"> <input type="hidden" name="id" value="$id"> Title: <input type="text" name="title" value="<? echo "$title"; ?>"><br> Duration: <input type="text" name="dur1" value="<? echo "$dur1"; ?>"><br> Price: <input type="text" name="price" value="<? echo "$price"; ?>"><br> Description: <input type="text" name="description" value="<? echo "$description"; ?>"><br> <input type="Submit" value="Update"> </form> edit1.php is the processing page <?php require_once("db.php"); $id = $_POST['id']; $ntitle = $_POST['title']; $ndur1 = $_POST['dur1']; $nprice = $_POST['price']; $ndescription = $_POST['description']; $result = mysql_query("UPDATE specialoffer SET title='$ntitle', dur1='$ndur1', price='$nprice', description='$ndescription' WHERE id='$id'") or die(mysql_error()); echo "Record Updated"; ?> pls guys dieing to get this working ? Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246874 Share on other sites More sharing options...
jdadwilson Posted May 7, 2007 Share Posted May 7, 2007 This is the best set of code I have found for generating clean sql statements. Put the function in an include so it can be referenced multiple places... function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": break; case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } Now for the code... $querySQL = sprintf("INSERT INTO myTable ( table_field_1, table_field_2, table_field_3, table_field_4, table_field_5) VALUES (%s, %s, %s, %s, %s)", GetSQLValueString($_GET['field1'], "text"), GetSQLValueString($_GET['field2'], "text"), GetSQLValueString($_GET['field3'], "text"), getSQLValueString($_GET['field4'], "text"), GetSQLValueString($_GET['field5'], "text")); You can use the same sprintf structure for UPDATE and DELETE. Good luck... Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246914 Share on other sites More sharing options...
Psycho Posted May 7, 2007 Share Posted May 7, 2007 You first need to stop and take a breath. When something is not working you need to start the debugging process. For example if you have an IF statemet which is not firing that you think should then you need to identify what the values of the varables are that you are testing in the IF statement - just echo them to the page. Ok, so you create new code and you ask for help in getting it working. At least explain what is or is not happening with the code. Also, you are still using that WHILE loop which makes no sense. Oh, and you should ALWAYS use comment in your code. Try this (not tested so there may be typos): <?php include("db.php"); if (isset($_POST[submit])) { $title = $_POST['title']; $dur1 = $_POST['dur1']; $description = $_POST['description']; $price = $_POST['price']; $id = $_POST['id']; $query = "UPDATE specialoffer SET title='$title', dur1='$dur1', price='$price', description='$description' WHERE id='$id'"; mysql_query($query,$con) or die (mysql_error()); echo "<b>Offer UPDATED Successfully!<br>You'll be redirected to admin page after (4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=admin.php>"; exit; } if (!$_GET[id]) { echo "No id passed to the page."; exit; } $id = $_GET[id]; $query = "SELECT * FROM specialoffer WHERE id='$id'"; $result = mysql_query($query,$con) or die (mysql_error()); $myrow = mysql_fetch_assoc($result)) $title = $myrow["title"]; $price = $myrow["price"]; $dur1 = $myrow["dur1"]; $desc = $myrow["description"]; ?> <br> <h3>::Edit News</h3> <form method="POST" action="<?=$_SERVER[php_SELF]?>"> <input type="hidden" name="id" value="<?=$id?>"> Title: <input name="title" size="40" maxlength="255" value="<?=$title?>"> <br> Duration: <input name="dur1" size="40" maxlength="255" value="<?=$dur1?>"> <br> Price Range: <input name="price" size="40" maxlength="255" value="<?=$price?>"> <br> Description: <textarea name="description" rows="7" cols="30"><?=$desc?></textarea> <br> <input type="submit" name="submit" value="Update Offer"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-246921 Share on other sites More sharing options...
illuz1on Posted May 8, 2007 Author Share Posted May 8, 2007 ok i see what you saying, and tried to look for the } and not there ... Parse error: parse error, unexpected ')' in /home/capetown/public_html/dev/edit.php on line 33 Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-248617 Share on other sites More sharing options...
Psycho Posted May 9, 2007 Share Posted May 9, 2007 That error is from this line: $myrow = mysql_fetch_assoc($result)) Change to: $myrow = mysql_fetch_assoc($result); Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-248740 Share on other sites More sharing options...
illuz1on Posted May 9, 2007 Author Share Posted May 9, 2007 thanks alot man really appreciated Quote Link to comment https://forums.phpfreaks.com/topic/50291-solved-desperate-to-get-update-query-working/#findComment-248973 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.