Insert Data Problem[Please Help]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joola
Forum Newbie
Posts: 8
Joined: Mon May 26, 2008 10:51 pm

Insert Data Problem[Please Help]

Post by joola »

Hi, I have problem..umm..perhaps a little bit problem that I can`t figured it out...below is my half script. I think the problem is in this part: (This script is part of a register form)

$day= $_POST['day'];
$sql = "INSERT INTO `rute` (`rute_id`, `from_id`, `to_id`, `flight_no`, `flight_time`, `flight_time2`, `via`, `type`, `day`)
VALUES ('$id', '$from', '$to, '$no', '$tm1', '$tm2', '$via', '$type' ' ";
for($i=0;$i<count($day);$i++){
$sql .= " ".$day[$i].", " ;
}
$sql .= "".$day[$i]." ')";


mysql_query($sql);

The colored text related to this select tags :

<select name="from" class="textField" id="from">
<option>--Arrival City--</option>
<?php
include "mainFunction.php";

$sql="SELECT * FROM `from_city` ORDER BY `from_id` ASC";
if (! $res=mysql_query($sql, $dbh)) {
echo mysql_error();
return 0;
}

$num=mysql_numrows($res);
$i=0;
while ($i < $num)
{
$row=mysql_fetch_row($res);
$id=mysql_result($res,$i,"from_id");
$city=mysql_result($res,$i,"city");
++$i;
print "<option value=$id>$city</option>";
}
?>
</select>

This Check box get the data from database. And then whe visitor checked this option the database will insert the data to Mysql. The Error message was :

error in executing query INSERT INTO `rute` (`rute_id`, `from_id`, `to_id`, `flight_no`, `tm1`, `tm2`, `via`, `type`, `day`) VALUES ('1', '1', '1', '24234', '11:02', '23:09', 'Idaho', 'B21-3423' ' 2, 3, ')

please click here to go back


Thanks a lot for any help...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Insert Data Problem[Please Help]

Post by Benjamin »

You are missing a comma after 'B21-3423'

There also seems to be an extra ' at the end of the query.
joola
Forum Newbie
Posts: 8
Joined: Mon May 26, 2008 10:51 pm

Re: Insert Data Problem[Please Help]

Post by joola »

astions wrote:You are missing a comma after 'B21-3423'

There also seems to be an extra ' at the end of the query.
I`ve been fixing the error, but there`s still errors...

error in executing query INSERT INTO `rute` (`rute_id`, `from_id`, `to_id`, `flight_no`, `tm1`, `tm2`, `via`, `type`, `day`) VALUES ('1', '1', '1', '3234-NDAS', '11:01', '23:09', 'Idaho', 'PN-2423', 1, 2, )
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Insert Data Problem[Please Help]

Post by Benjamin »

The end of your query has a , with nothing after it. You can retrieve details of the last error with the mysql_error() function.

http://us2.php.net/manual/en/function.mysql-error.php
joola
Forum Newbie
Posts: 8
Joined: Mon May 26, 2008 10:51 pm

Re: Insert Data Problem[Please Help]

Post by joola »

astions wrote:The end of your query has a , with nothing after it. You can retrieve details of the last error with the mysql_error() function.

http://us2.php.net/manual/en/function.mysql-error.php
I used that function, and showed up this sign on frontend page:

0:
0:

I think theres something wrong with this script :

$day= $_POST['day'];
$sql = "INSERT INTO `rute` (`rute_id`, `from_id`, `to_id`, `flight_no`, `flight_time`, `flight_time2`, `via`, `type`, `day`)
VALUES ('$id', '$from', '$to, '$no', '$tm1', '$tm2', '$via', '$type' ' ";
for($i=0;$i<count($day);$i++){
$sql .= " ".$day[$i].", " ;
}
$sql .= "".$day[$i]." ')";

I think something wrong with (") and (') position....could you help me to solve this??

Thanks...Best Regards,
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Insert Data Problem[Please Help]

Post by Benjamin »

1. Please use [code = php] tags when posting code. (without spaces)
2. This is untested. The code you posted is very insecure. Please use mysql_real_escape_string or other measures to prevent sql injection.

Code: Select all

 
$day = $_POST['day'];
 
$sql = "INSERT INTO `rute` (`rute_id`, `from_id`, `to_id`, `flight_no`, `flight_time`, `flight_time2`, `via`, `type`, `day`)
VALUES ('" . mysql_real_escape_string($id) . "', '" . mysql_real_escape_string($from) . "', '" . mysql_real_escape_string($to) . ", '" . mysql_real_escape_string($no) . "', '" . mysql_real_escape_string($tm1) . "', '" . mysql_real_escape_string($tm2) . "', '" . mysql_real_escape_string($via) . "', '" . mysql_real_escape_string($type) . "', '";
 
for($i = 0; $i <= count($day); $i++)
{
    $sql .= mysql_real_escape_string($day[$i]) . ",";
}
 
$sql = substr($sql, 0, -1) . "')";
 
echo $sql;
 
If the query fails please post the query, your table structure and the results of mysql_error().
joola
Forum Newbie
Posts: 8
Joined: Mon May 26, 2008 10:51 pm

Re: Insert Data Problem[Please Help]

Post by joola »

The error is not in that script but the "if" condition that I wrote under that script above. There`s variabel that not called and then gave the error output. I`m sorry from my mistaken...Thank You So Much for the respond...quite fast too... :D :wink: :drunk: Thanks a lot...
Post Reply