Jump to content

Recommended Posts

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
?>


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.

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 ? :(

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...

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>

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.