PHP Insert script

Inserting command is not working properl

Page 1 of 1

2 Replies - 8816 Views - Last Post: 16 May 2006 - 10:30 AM Rate Topic: -----

#1 david4u  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 26-April 06

PHP Insert script

Posted 15 May 2006 - 02:25 AM

Can anyone help with is code. I cannot insert values into Questionnaire....

I have tried using different methods but it is not working. Is there any error that I have made?

This is the main problem:


$query = "INSERT INTO Questionnaire VALUES (' ', '$unit_id', '$quest_id')";
$result = mysql_query($query);


.........................................................................................................

Part of the code start here



[ php ] $qtype_id = $_POST['qtype_id'];
//$unit_id = $_POST['unit_id'];


$query = "INSERT INTO Questions VALUES ( ' ', '$q', '$qtype_id')";
$result = mysql_query($query);
$quest_id = mysql_insert_id();

$query2 = "SELECT unit_id FROM Units WHERE unit_name='B302'";
//$result = mysql_query ($query);
$result = mysql_query ("SELECT unit_id FROM Units") or die ("Invalid query");
$unit_id = mysql_insert_id();

$query = "INSERT INTO Questionnaire VALUES (' ', '$unit_id', '$quest_id')";
$result = mysql_query($query);

if ($result) { // if it ran ok

echo '<p>Question is registered in database.</p>';
exit(); //Quit script.
} else { // Something is not right.
$message = '<p>Please go back and refill form.</p><p> ' . mysql_error() . '</p>';
}
mysql_close(); // Close database connection
} else { // Display the form.

}

// Print the error message if there is one
if(isset($message)) {
echo '<font color="red">', $message, '</font>'; [ /php]

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Insert script

#2 jaelle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-April 05

Re: PHP Insert script

Posted 16 May 2006 - 10:20 AM

When I have problems like this, I try running the query in phpMyAdmin for the database. It will often give you a general idea where the error is coming from.

From your code I can't see any obvious errors. Maybe check to see if your data that you are inserting contains a ' character.. mySQL tends to throw a fit over that. I suppose another problem could be if your data you are trying to insert doesn't match up up the fields available in the table...
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: PHP Insert script

Posted 16 May 2006 - 10:30 AM

Always use addslashes() on variables you're inserting into a database. It'll help keep errors down as well as help protect you from malicious intents.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1