Jump to content

Insert query not working


nomadsoul

Recommended Posts

Hi,

This form is not inserting into the quiz table.  I know that MySql is very strict about inserts, especially with PKs and auto_incs. Is this the correct syntax?

The insert works on the CLI and the select and update queries appear on the forms.

//not my script but I'd like to use it

 

<HTML>
<link rel="stylesheet" href="quiz.css" type="text/css">
<body><center>
<P> </P>

<B>Admin area - edit the quiz</B>
<?php
include("contentdb.php");
if($submit)
{
$sql = "INSERT INTO $table (question, opt1, opt2, opt3, answer) VALUES ('$question','$opt1','$opt2','$opt3','$answer')";
$result = mysql_query($sql);
echo "<br><br>Question added to quiz.<br><br>";
include "qinsert.php";
}
else if($update)
{
$sql = "UPDATE $table SET question='$question',opt1='$opt1',opt2='$opt2',opt3='$opt3',answer='$answer' WHERE id=$id";
$result = mysql_query($sql);
echo "<br><br>The quiz has been succesfully updated.<br><br>\n";
}
else if($id)
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type="hidden" name="id" value="<?php echo $myrow["id"]?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $myrow["question"]?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $myrow["opt1"]?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $myrow["opt2"]?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $myrow["opt3"]?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $myrow["answer"]?>" size="30">
    <br>
    <br>
<input type="Submit" name="update" value="Update information"></form>
<?

}
else
{
?>
<form method="post" action="<?php echo $PHP_SELF?>">
    <p><br>
      <b>Question:</b><br>
      <input type="Text" name="question" size="50">
      <br>
      <b>Option 1:</b><br>
      <input type="Text" name="opt1" size="30">
      <br>
      <b>Option 2:</b><br>
      <input type="Text" name="opt2" size="30">
      <br>
      <b>Option 3:</b><br>
      <input type="Text" name="opt3" size="30">
      <br>
      <b>Answer</b> (must be identical to correct option):<br>
      <input type="Text" name="answer" size="30">
      <br>
      <br>
      <input type="Submit" name="submit" value="Enter information">
    </p>
    </form>
<?
}
?>
<a href="editquizlist.php">Back to list of quiz questions</a>
</center>
</body>
</HTML>

 

Describe quiz:

 

 Field    | Type       | Null | Key | Default | Extra          |
+----------+------------+------+-----+---------+----------------+
| id       | tinyint(4) | NO   | PRI | NULL    | auto_increment |
| question | text       | NO   |     | NULL    |                |
| opt1     | text       | NO   |     | NULL    |                |
| opt2     | text       | NO   |     | NULL    |                |
| opt3     | text       | NO   |     | NULL    |                |
| answer   | text       | NO   |     | NULL    |                |
+----------+------------+------+-----+---------+----------------+

Link to comment
https://forums.phpfreaks.com/topic/181580-insert-query-not-working/
Share on other sites

The posted code is way out of date. It is dependent on register_globals being on to magically populate program variables form the $_POST data. register_globals were turned off by default in php4.2 in April of the year 2002, over 7 years ago. Since register_globals have been complete removed in php6, you would need to update the code for it to be usable with register_globals turned off in all current versions of php and removed in future versions.

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.