Jump to content

Small insert problem


emediastudios

Recommended Posts

Can someone pick whats wrong with my code please

 

if($_SESSION[passed] == "y"){

$id=$_POST['id'];
$title=$_POST['title'];
$date=$_POST['date'];
$text=$_POST['text'];


$query = "INSERT INTO news id='NULL', title='$title', date='$date', text='$text'";
$result = mysql_query($query) or die(mysql_error());

header("Location: admin.php"); 
}
  else{ 
  echo "You Must Be Logged In To Do That";
  }

Link to comment
https://forums.phpfreaks.com/topic/132250-small-insert-problem/
Share on other sites

Can you give us a hint? What's it doing? What's it supposed to do?

 

A quick glance shows me that

<?php
query = "INSERT INTO news id='NULL', title='$title', date='$date', text='$text'";
?>

is incorrect, try

<?php
query = "INSERT INTO news SET id='NULL', title='$title', date='$date', text='$text'";
?>

 

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/132250-small-insert-problem/#findComment-687514
Share on other sites

<?php
include_once('includes/include.php');
$password = "*******";


if($_POST[password] == "" && $_SESSION[passed] != "y"){
  $content .= "<form method='post' action='admin.php'><input type='password' name='password' value='password'><input type=submit></form>";
}
else if($_POST[password] != ""){
  if($password == $_POST[password]){
   $_SESSION[passed] = "y";
  }
  else{
    echo "<script>window.location = 'admin.php';</script>";
  }
}
if($_SESSION[passed] == "y"){


$title=$_POST['title'];
$date=$_POST['date'];
$text=$_POST['text'];


$query = "INSERT INTO news SET id='', title='$title', date='$date', text='$text'";
$result = mysql_query($query) or die(mysql_error());

header("Location: admin.php"); 
}
  else{ 
  echo "You Must Be Logged In To Do That";
  }

?>

 

My form looks like this

 

<?php
$content .= '<form action="news_edit.php" method="post"><table width="500">
  <tr>
    <td colspan="2"><span class="bolder">Manage News</span></td>
    </tr>
  <tr>
    <td><span class="bolder">Title</span></td>
    <td>
      <input type="text" name="title" id="title" />
</td>
  </tr>
  <tr>
    <td><span class="bolder">Date</span></td>
    <td><input type="text" name="date" id="date" /></td>
  </tr>
  <tr>
    <td valign="top"><span class="bolder">News</span></td>
    <td>
      <textarea name="text" id="text" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td><input name="id" type="hidden" value="id" /></td>
    <td><input type="reset" name="reset" id="reset" value="Reset" />
      <input type="submit" name="submit" id="submit" value="Submit" /></td>
  </tr>
</table>
</form>';
}
?>

 

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/132250-small-insert-problem/#findComment-687527
Share on other sites

If the field "id" in your table is of type "int" and set to "autoincrement", then you can just leave it out of the mysql insert statement:

<?php
$query = "INSERT INTO news SET title='$title', date='$date', text='$text'";
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

 

I also find it very helpful to include the query in the die() statement.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/132250-small-insert-problem/#findComment-687652
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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