2013-03-04 164 views
0

你好我有一個Html表單發送0而不是1到我的數據庫的問題,它被設置爲tinyint 1 我一直在搞這個好幾天。HTML複選框表格php

我用phpmyadmin XAMPP服務器HTML PHP

<!--THIS IS SKILLS--> 
<?php 



$con = mysql_connect("localhost","root",""); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
if (!isset($_POST['action'])) 

mysql_select_db("client", $con); 
$sql="INSERT INTO skills (driving_licence,HGV,engineer,carpenter,chef,PSV) 
VALUES 
('$_POST[driving_licence]','$_POST[HGV]','$_POST[engineer]','$_POST[carpenter]','$_POST[chef]','$_POST[PSV]')"; 

if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
echo "skills have been added"; 

mysql_close($con); 

?> 

表單代碼是

<center><form method="post" action="skills.php"> 
<table cellspacing="50"> 
<tr> 
<center><h1>Record Client Skills</h1></center> 
<center><p> Please choose the relevent boxes</p></center> 


<center><input type="checkbox" name="driving_licence" value="driving_licence">I have a Full UK Driving Licence<br> 
<input type="checkbox" name="hdv" value="hgv">I hold a valid a HGV licence<br> 
<input type="checkbox" name="engineer" value="engineer">I have an Engineering qualification <br> 
<input type="checkbox" name="carpenter" value="carpenter">I have a Carpentry qualification <br> 
<input type="checkbox" name="chef" value="chef">I have Catering qualification<br> 
<input type="checkbox" name="psv licence" value="psv">I have a valid PSV licence<br> 
</tr> 
</table> 

<input type="reset" value="Reset Form"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="Send" /> 


</center> 

誰能幫助?

羅布

+1

我就不會擔心了0/1的東西,擔心張開大開[SQL注入漏洞(HTTP://鮑比桌。 com)在你的代碼中。但請記住,表單中未選中的複選框在提交表單時通過網絡發送** NOT **。 – 2013-03-04 15:07:59

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – 2013-03-04 15:08:05

+1

我認爲你的tinyint列的默認值爲0,現在試圖將字符串添加到此列。 – Tim 2013-03-04 15:08:33

回答

0

設置複選框value="1",否則串"driving_license"傳遞到您的DB-場是tinyint :-(

和:移動到庫MySQLi或PDO,MySQL是過時

+0

謝謝你會做 – pitbull10 2013-03-04 15:15:44

0

,最好使用bit(1)而不是tinyint作爲布爾值,不要使用root連接到mysql並使用PDO或mysqli,最後它不是用於爲用戶打印mysql錯誤的好方法,請使用php數據庫類之一

0
<?php 



$con = mysql_connect("localhost","root",""); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
if (isset($_POST['submit'])) 
{ 
mysql_select_db("client", $con); 
$sql="INSERT INTO skills (driving_licence,HGV,engineer,carpenter,chef,PSV) 
VALUES 
('$_POST[driving_licence]','$_POST[HGV]','$_POST[engineer]','$_POST[carpenter]','$_POST[chef]','$_POST[PSV]')"; 

if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
echo "skills have been added"; 

mysql_close($con); 
} 
?> 

HTML表單 -

<center><form method="post" name="f1" action="skills.php"> 
<table cellspacing="50"> 
<tr> 
<center><h1>Record Client Skills</h1></center> 
<center><p> Please choose the relevent boxes</p></center> 
<center><input type="checkbox" name="driving_licence" value="driving_licence">I have a Full UK Driving Licence<br> 
<input type="checkbox" name="hdv" value="hgv">I hold a valid a HGV licence<br> 
<input type="checkbox" name="engineer" value="engineer">I have an Engineering qualification <br> 
<input type="checkbox" name="carpenter" value="carpenter">I have a Carpentry qualification <br> 
<input type="checkbox" name="chef" value="chef">I have Catering qualification<br> 
<input type="checkbox" name="psv licence" value="psv">I have a valid PSV licence<br> 
</tr> 
</table> 
<input type="reset" value="Reset Form"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="Send" name="submit" /> 
</center>