SQL语法错误,自己找不到

时间:2013-01-01 20:16:14

标签: php html mysql forms

  

可能重复:
  MySQL - when to use single quotes, double quotes, and backticks?

我已经盯着自己瞎了眼。并且需要一些帮助才能找到错误。

HTML:

        <b>Navn:</b> 
        <input type="text" name="navn" id="navn">

        </br>
        </br>

        <b>K&oslash;n:</b>
        <input type="radio" name="køn" value="mand" id="radio">Mand
        <input type="radio" name="køn" value="kvinde">Kvinde

        </br>
        </br>

        <b>Alder:</b> 
        <select name="alder" id="selecta">
        <option value="blank"></option>
        <option value="10til15">10 til 15 &aring;r</option>
        <option value="15til20">15 til 20 &aring;r</option>
        <option value="20til25">20 til 25 &aring;r</option>
        <option value="25til30">25 til 30 &aring;r</option>     
        </select>

        </br>
        </br>

        <b>V&aelig;gt:</b>  
        <select name="vægt" id="selectv">
        <option value="blank">Vægt</option> 
        <option value="30til40">30 til 40 kg</option>
        <option value="40til50">40 til 50 kg</option>
        <option value="50til60">50 til 60 kg</option>
        <option value="60til70">60 til 70 kg</option>       
        </select>

        </br>
        </br>

        <b>B&aelig;lte:</b>
        <select name="bælte" id="selectb">
        <option value="blank">vægt</option>
        <option value="hvid">Hvid</option>
        <option value="grøn">Gr&oslash;n</option>
        <option value="blå">Bl&aring;</option>
        <option value="sort">Sort</option>      
        </select>

PHP:

$navn = $_POST['navn'];
$køn = $_POST['køn'];
$alder = $_POST['alder'];
$vægt = $_POST['vægt'];
$bælte = $_POST['bælte'];

$con = mysql_connect("...", "...", "...") or die(mysql_error());
echo "Connected to MySQL<br />";

mysql_select_db("...", $con) or die(mysql_error());
echo "Connected to Database<br />";

mysql_query("INSERT INTO Tilmeldinger 
(column 2, column 3, column 4, column 5, column 6)
VALUES ($navn, $køn, $alder, $vægt, $bælte)") or die(mysql_error());

mysql_query("SELECT * FROM Tilmeldinger ORDER BY column 3, column 4, column 5, column 6") or die(mysql_error());

mysql_close;

echo "Tak for din tilmelding! Du bliver viderstillet om et kort øjeblik";

header("Refresh: 5; URL=http://www.casperwmn.dk/tilmelding.php");
exit; }

我得到了输出:

Connected to MySQL
Connected to Database
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 'column 2, column 3, column 4, column 5, column 6) VALUES (Casper, mand, 20til25,' at line 2

我已经超过了有价值的&amp;vægt,但我似乎无法找到问题所在?

事先,谢谢!

4 个答案:

答案 0 :(得分:1)

而不是第2列,请尝试`column 2`。在列名称周围添加反引号。

答案 1 :(得分:1)

可能应该是:

mysql_query("INSERT INTO Tilmeldinger 
(column2, column3, column4, column5, column6)
VALUES ($navn, $køn, $alder, $vægt, $bælte)") or die(mysql_error());

答案 2 :(得分:1)

在SQL语句中,除非引用它们,否则列名称不能包含空格。 E.g:

INSERT INTO (`column 2`, `column 3`, ...) VALUES ( ... )

同样在SELECT中,列名必须在ORDER BY子句中引用,因为它们包含空格。

答案 3 :(得分:0)

正如你的错误所说

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 'column 2, column 3, column 4, column 5, column 6) VALUES (Casper, mand, 20til25,' at line 2

- &GT; 靠近'第2列,第3列,第4列,第5列,第6列)VALUES(Casper,mand,20til25,'在第2行

我猜,问题是,你没有将列名转换为'-characters,如'column n'

试一试......