By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Failed to insert data on mysql

ddtpmyra
100+
P: 333
Hello,

Im having problem and I dont know why where it coming from, maybe you can help.

Files:
create_topic.php

[HTML]<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6" align="center"><strong><font size="4">Create New Topic</font></strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="85%"><input name="topic" type="text" id="topic" size="60" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="20" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>

<td align="center"><input name="id" type="hidden" id="id" value="<?$id=$_GET['id']; echo "$id"?>">
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" />
</td> <td>&nbsp; </td>


</tr>
</table>
</td>
</form>[/HTML]

File: Add_topic.php

[PHP]<?php
$host="localhost"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="cmr"; // Database name
$tbl_name="forum_question"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$id=$_POST['id'];


$datetime=date("d/m/y h:i:s"); //create date time

$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime, cmrid)
VALUES('$topic', '$detail', '$name', '$email', '$datetime','$id')";
$result=mysql_query($sql);
if($result){
echo "<font size='3' face='Times New Roman'>Successful</font><br>";
echo "<p><a href='main_forum.php?id={$id}'><font size='4' face='Times New Roman, Times, serif'>View your topic</font></a>";
}
else {
echo "ERROR"; //HERE WHERE QUERY FALL

}
mysql_close();
?>[/PHP]

DISPLAY MESSAGE:
'ERROR'
Oct 24 '08 #1
Share this Question
Share on Google+
5 Replies


Markus
Expert 5K+
P: 6,050
I am not prepared to look through that bundle of code with such a small amount of information. Remember: this is a free forum where users devote their time to help other users. You have to be as helpful as possible or you will not get any help.

One thing I do suggest is do this with your mysql_query()ies

Expand|Select|Wrap|Line Numbers
  1. $sql = "some sql code";
  2. mysql_query($sql) or die(mysql_error()); // this line will give an error if there is a problem with the query.
  3.  
Oct 24 '08 #2

nathj
Expert 100+
P: 938
Hi,

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. $sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime, cmrid)
  2. VALUES('" . $topic . "', '" . $detail ."', '" . $name . "', '" . $email. "', '" . $datetime . "', $id)" ; // assuming $id is numeric if not when you create it from the $_POST array wrap it in intVal
  3.  
this may help, it may not, I have no idea what the rror is.

It could be that the variables are not being set at al. I just thought I'd take a guess. do what Markus said - it makes the most sense.

nathj
Oct 24 '08 #3

chelvan
P: 90
hi
i'm afraid on your if condition.

here you wish to check that the query was execute or not.

if so try like this
before insert count number of rows on your table
do the query
count number of rows again on your table

then check both counter value


regards
chel-1
Oct 24 '08 #4

ddtpmyra
100+
P: 333
I am not prepared to look through that bundle of code with such a small amount of information. Remember: this is a free forum where users devote their time to help other users. You have to be as helpful as possible or you will not get any help.

One thing I do suggest is do this with your mysql_query()ies

Expand|Select|Wrap|Line Numbers
  1. $sql = "some sql code";
  2. mysql_query($sql) or die(mysql_error()); // this line will give an error if there is a problem with the query.
  3.  

I tried Markus suggestion and Nathj was right it didn't capture my $id on page that calling my query and here's the error message
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 'id'];?>')' at line 1
and here's what I did to capture the id value from url I wonder what's wrong with this, everythings is working until I move my script to a Windows Server before it was stored and running on 'Novell' do I have do some more configuration on my httpd?
[PHP]<input name="id" type="hidden" id="id" value="<? $id=$_GET['id'];?>" />[/PHP]
Oct 24 '08 #5

pbmods
Expert 5K+
P: 5,821
Heya, ddtpmyra.

Sounds like your PHP code is getting output rather than executed.

If you are outputting your <input> via PHP, you need to change your syntax:
Expand|Select|Wrap|Line Numbers
  1. echo '<input ... value="' . $_GET['id'] . '" />';
  2.  
To debug your query, try echoing it to make sure you're passing in what you think you're passing in:
Expand|Select|Wrap|Line Numbers
  1. echo $sql;
To prevent SQL errors, also consider using mysql_real_escape_string() (http://php.net/mysql_real_escape_string).
Oct 25 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.