473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Failed to insert data on mysql

ddtpmyra
333 100+
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
5 4275
Markus
6,050 Expert 4TB
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
938 Expert 512MB
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
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
333 100+
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
5,821 Expert 4TB
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

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

Similar topics

2
by: Atz | last post by:
Hi to all ! Im using MySql front and PHP 5 for some web shop. I didn't try it so far but i guess that the online data insertation ( accept costs and time ) should be the same process like for...
2
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below...
1
by: dimitri | last post by:
i try to copy data from excel into my MySql database which runs on a linux. i use MS Access to access the database. i need to optain the unique id number, and this is where i have some troubles....
6
by: VitaminB | last post by:
Hi Guys, I try to insert data from a form into a mysql database, but it did not work... There is no error, but the data did not pass thru the table. This is the short script:
5
seshu
by: seshu | last post by:
hi everybody this is seshu i am creating an application for chekin/checkouttime for that i need to insert data in to mysql for that i know the command in mysql and even how to do...
3
by: Waruna | last post by:
Is there a way to block insert into mysql(5.0) using c api of mysql db.. i.e. say there is a table with 2 columns, one contains char other int then i want to insert 500 records at once,, as i...
4
by: vijjibalu | last post by:
Hi all, as part of my project requirement we are migrating procedures from mssql to mysql below is the code in mssql CREATE PROCEDURE TABLE_DEPEND_INSERT_ORDER AS CREATE TABLE...
5
ddtpmyra
by: ddtpmyra | last post by:
I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql Scenario: I have 3 checkbox on my form and...
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
1
by: neelsbr | last post by:
Error getting when i insert data after truncate the table.once more i truncate that table data is going otherwise it will show the error message. this is the error message i am getting .."Failed to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.