Jump to content

Need help with this one script..


2NR

Recommended Posts

for some reason. everything i go to UPDATE an existing entry it doesnt want to update, but deleting and adding new "years" works completely fine :)

 

this code it killin meeeeeeeeeeeeee

 

thanks in advance!

 

<?php
/**** Dealing with the database ****/
// connect to db
$conn = mysql_connect('localhost','user','pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('cars',$conn) or trigger_error("SQL", E_USER_ERROR);

// INSERT: if we have a year to add...
if($_POST['year']) {
   // little bit of cleaning...
   $year = mysql_real_escape_string($_POST['year']);
   // insert new year into table
   $sql = "INSERT INTO CARS (carID, year) VALUES ('','$year')";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if

// UPDATE: if we have years(s) to change... 
if($_POST['cyear']) { 
   // for each year to change... 
   foreach($_POST['cyear'] as $ccarID => $cyear) { 
     // little bit of cleaning... 
      $carID = mysql_real_escape_string($ccarID); 
      $year = mysql_real_escape_string($cyear); 
      // update year in the table   
      $sql = "UPDATE CARS SET year = '$year' WHERE carID = '$carID'"; 
      $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); 
   } // end foreach 
} // end if 

// DELETE: if we have a name to delete...
if($_GET['year']) {
   // little bit of cleaning...
   $year = mysql_real_escape_string($_GET['year']);
   // delete year from table
   $sql = "DELETE FROM CARS WHERE year = '$year'";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if

// ORDERBY: if one of the links was clicked..
if ($_GET['orderby']) {
   // make an aray of allowed names
   $allowed = array('carID','year');
   // bit of cleaning...
   $order = mysql_real_escape_string($_GET['orderby']);
   // is it a valid column name? yes: use it. no: default to 'carID'
   $order = (in_array($order, $allowed))? $order : "carID";
// if no link clicked, default to 'carID'
} else {
   $order = "carID";
} // end else

// SELECT: get the list of year from database
$sql = "SELECT carID, year FROM CARS ORDER BY $order";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
/**** end deal with the database ****/

/**** list everything out ****/
// list columns
echo <<<LISTCOLS
<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>
<table border = '1'>
   <tr>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=carID'>carid</td>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=year'>year</td>
      <td>delete</td>
   </tr>
LISTCOLS;

// loop through list of names
while ($list = mysql_fetch_assoc($result)) {
echo <<<LISTINFO
   <tr>
      <td>{$list['carID']}</td>
      <td><input type = 'text' year = 'cyear[{$list['carID']}]' value = '{$list['year']}'>
      <td><a href = '{$_SERVER['PHP_SELF']}?year={$list['year']}'>delete</a></td>
   </tr>
LISTINFO;
} // end while

// list input box for adding new entry
echo <<<NEWENTRY
   <tr>
      <td bgcolor = 'gray'></td>
      <td><input type = 'text' name = 'year'></td>
      <td bgcolor = 'gray'></td>
   </tr><tr>
      <td></td>
      <td align = 'center'><input type = 'submit' value = 'submit'></td>
      <td></td>
   </tr>
</table>
</form> 
NEWENTRY;
/**** end list everything out ****/

?>

Link to comment
Share on other sites

Echo $sql after this line to verify your variables

 

$sql = "UPDATE CARS SET year = '$year' WHERE carID = '$carID'";

 

 

ok so i did

 

$sql = "UPDATE CARS SET year = '$year' WHERE carID = '$carID'";
echo $sql;

 

right? if so NOTHING showed up.. :(

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.