Jump to content

[SOLVED] Problem inserting into database


XeroXer

Recommended Posts

Start with the problem stuff:

[b]database[/b]
[code]CREATE TABLE `support_questions` (
`id` tinyint(9) NOT NULL auto_increment,
`username` varchar(99) NOT NULL default '',
`email` varchar(99) NOT NULL default '',
`question` longtext NOT NULL,
`qdate` varchar(99) NOT NULL default '',
`answer` longtext NOT NULL,
`adate` varchar(99) NOT NULL default '',
`ausername` varchar(99) NOT NULL default '',
`show` varchar(99) NOT NULL default '',
PRIMARY KEY  (`id`)
) TYPE=MyISAM ;[/code]

[b]form[/b]
[code]<?php
echo "<table width='50%' aling='left' valign='top'><tr><td>";
echo "<form id='login' name='login' method='post' action='index.php?p=ask'>";
echo "<input type='text' name='user' value='username' onFocus='formrule(this);'><br>";
echo "<input type='text' name='email' value='e-mail' onFocus='formrule(this);'><br>";
echo "<textarea name='quest' onFocus='formrule(this);'>question</textarea><br>";
echo "<input type='submit' name='ask' value='ask'>";
echo "</form>";
echo "</td></tr></table>";
?>[/code]

[b]code[/b]
[code]<?php
include("database.php");
$con = mysql_connect("$mysqlhost","$mysqlusr","$mysqlpass") or die('Could not connect: ' . mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());
mysql_query("INSERT INTO support_questions (username, email, question, show) VALUES ('$name', '$email', '$message', '$thetime', '0')");
echo "<table width='50%' aling='left' valign='top'><tr><td>";
echo "Question has been asked. Answer will be updated by an admin.";
echo "<br><br><u>Save this information to get your answer later.</u>";
echo "<br><b>Name:</b> ";
echo $name;
echo "<br><b>E-mail:</b> ";
echo $email;
echo "<br><b>Question:</b> <table><tr><td>";
echo $message;
echo "</td></tr></table></td></tr></table>";
mysql_close($con);
?>[/code]

[hr]

I have been sitting with this code on the side all day.
Checking it a few times and it just won't work.
Probebly I have just missed a variable like the last time.
Please help...

[i]The part I find strange is that i copied the code from my other site where it works.
So I can't find the problem...[/i]
Link to comment
https://forums.phpfreaks.com/topic/33004-solved-problem-inserting-into-database/
Share on other sites

Hi If you had a warning script at the end of your mysql_query statement such as

[code]
<?php
mysql_query("INSERT INTO support_questions (username, email, question, show) VALUES ('$name', '$email', '$message', '$thetime', '0')") or die ('Error in query: ' . mysql_error());
?>
[/code]
it would probably have said something like there was an error in your query.
INSERT INTO (username, email, question, show) // 4 field names
VALUES ('$name', '$email', '$message', '$thetime', '0') // 5 variables

too many entries for the number of fields

hope that helps
changed that row to:
[code]<?php
mysql_query("INSERT INTO support_questions (username, email, question, qdate, show) VALUES ('$name', '$email', '$message', '$thetime', '0')") or die ('Error in query: ' . mysql_error());
?>[/code]

but then I get the error:
[code]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'show) VALUES ('XeroXer', '[email protected]', 'hello', '2007-01-0[/code]
The word "show" is most likely a reserved word in MySQL so you need to enclose it in backticks:
[code]<?php
mysql_query("INSERT INTO support_questions (username, email, question, qdate, `show`) VALUES ('$name', '$email', '$message', '$thetime', '0')") or die ('Error in query: ' . mysql_error());
?>[/code]

Ken

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.