无法使用mysql_query()提交查询

时间:2013-06-14 06:40:53

标签: php mysql

我是新来的,这是我的第一个问题! 我在mysql和php中遇到了困难

<?php
echo "Hello World";
$con=mysql_connect('localhost:3306','dmail','*****','dhruv');    
if(!$con)
{
echo "Failed to connect"; }

$name2 = 'name2';
$tel_no2 = 'tel_no2';
$email2 = 'email2';
$query2 = 'query2';
$car = 'car';
$city2 = 'city2';
$country2 = 'country2';
$date = 'date';

$query1 ="INSERT INTO 'booking' VALUES (name2, tel_no2, email2, city2, country2, car,         date, query2)";
$query2 ="INSERT INTO 'booking' VALUES ('$name2', '$tel_no2', '$email2', '$city2',     '$country2', '$car', '$date', '$query2')"; 

$update = mysql_query($query1,$con);
if(!$update)
{    echo "Failed to update"; }

>

它总是显示“无法更新”,任何帮助将不胜感激。 感谢。

3 个答案:

答案 0 :(得分:2)

$con=mysql_connect('localhost:3306','dmail','*****','dhruv'); 

应该是

$con=mysql_connect('localhost:3306','dmail','*****'); 
$db_selected = mysql_select_db('dhruv', $con);

答案 1 :(得分:1)

删除table_name周围的'并围绕值添加'$

$query1 ="INSERT INTO `booking` VALUES ('$name2', '$tel_no2', '$email2', '$city2', '$country2', '$car','$date', '$query2')";
$query2 ="INSERT INTO `booking` VALUES ('$name2', '$tel_no2', '$email2', '$city2',     '$country2', '$car', '$date', '$query2')"; 

答案 2 :(得分:0)

您应该在mysql查询中使用$来实际将值插入表中。所以:

//This query is not valid since you are neither passing a string nor a variable
$query1 ="INSERT INTO 'booking' VALUES (name2, tel_no2, email2, city2, country2, car,         date, query2)";

所以这应该用这样的单引号括起来(以字面顺序传递name2tel_no2等):

$query1 ="INSERT INTO 'booking' VALUES ('name2', 'tel_no2', 'email2', 'city2', 'country2', 'car', 'date', 'query2')";

或者您可以传递变量的值,如下所示:

$query2 ="INSERT INTO 'booking' VALUES ('$name2', '$tel_no2', '$email2', '$city2',     '$country2', '$car', '$date', '$query2')";