2013-07-28 59 views
-4

我是新手PHP函數不運行

下面

是我的HTML PHP形式

<div id="user"> 
<form name="booksInput" action="theGamer.php" method="post"> 
<p>This Section is to add new USer Record in DB</p> 
User F Name: <input type="text" name="Fuser"> 
User L Name: <input type="text" name="Luser"> 
<?php 
$con=mysql_connect("localhost","root","","library"); 
mysql_select_db("library",$con); 
$sqlb = "SELECT * FROM books"; 
$queryb = mysql_query($sqlb); 
while ($resultsb[] = mysql_fetch_object ($queryb)); 
?> 
Book Name:<select name="bID"> 
<?php foreach ($resultsb as $optionb) { ?> 
<option value="<?php echo $optionb->bID; ?>"><?php echo $optionb->book;?></option> 
<?php } ?> 
</select><br> 
<?php    
$con=mysql_connect("localhost","root","","library"); 
mysql_select_db("library",$con); 
$sqlc = "SELECT * FROM country"; 
$queryc = mysql_query($sqlc); 
while ($resultsc[] = mysql_fetch_object ($queryc)); 
array_pop($resultsc); 
?> 
Country Name:<select name="cID"> 
<?php foreach ($resultsc as $optionc) : ?> 
<option value="<?php echo $optionc->cID; ?>"><?php echo $optionc->country;?>/option 

    <?php endforeach; ?> 
</select> 
<input type="submit" value="Submit" name="subUser"> 
</form> 
    </div> 

現在當按鈕nameing sunUser按下theGamer.php定向和theGamer.php代碼如下

提交
<?php 
$con=mysql_connect("localhost","root","","library"); 
function insertBook() 
{ 
global $con; 
mysql_select_db("library",$con); 
$book=mysql_real_escape_string($_POST['books']); 
$sql="INSERT INTO books (bID, book) VALUES ('','$book')"; 
if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error($con)); 
}` 
echo "1 record added"; 
} 

function insertCountry() 
{ 
global $con; 
mysql_select_db("library",$con); 
$country=mysql_real_escape_string($_POST['country']); 
$sql="INSERT into country (cID, country) VALUES ('','$country')"; 

if (mysql_query($sql,$con)) 
{ 
echo "1 record added"; 
} 
else{die('Error: ' . mysql_error($con));} 

} 

function insertUser() 
{ 
$con=mysql_connect("localhost","root","","library"); 
mysql_select_db("library",$con); 
$Fuser=mysql_real_escape_string($_POST['Fuser']); 
$Luser=mysql_real_escape_string($_POST['Luser']); 
$bID=mysql_real_escape_string($_POST['bID']); 
$cID=mysql_real_escape_string($_POST['cID']); 
$sql="INSERT INTO user (userid, uFname,uLname, bID, CID) VALUES 
    ('','$Fuser','$Luser','bID','cID')"; 
if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error($con)); 
} 
echo "1 record added"; 
    } 

    //Code for Knowing which button is pressed 
    if(isset($_POST['subBooks'])) 
    { 
    insertBook(); 
    } 
if(isset($_POST['subCountry'])) 
{ 
insertCountry(); 
} 
if(isset($_POST['subUser'])) 
{ 
insertUser(); 
} 
?> 

它給出了這個錯誤 解析錯誤:解析錯誤,期待T_STRINGT_VARIABLEC:\wamp\www\library\theGamer.php在線20 T_NUM_STRING wheras行號20有關於因爲subUser按鈕被按下,if語句一定要去u=insertuser功能爲什麼它給人錯誤在第20行

問候 鴨

NP關注
+2

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**警告**](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)。 –

+3

您錯誤信息中解釋了您'E_DOESNT_WORK'的原因。 –

+0

看看stackexchange的語法高亮顯示對你的代碼做了什麼。這應該給你提示有什麼不對。 – Oswald

回答

0
$book=mysql_real_escape_string($_POST['books']); 
$sql="INSERT INTO books (bID, book) VALUES ('','$book')"; 
if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error($con)); 
}` <--- Here is a rogue quote.. 

如果StackOverflow發現此問題,則使用語法highlighter。你是否也使用熒光筆編輯器?

由於代碼現在無效,PHP無法解析文件並且會失敗。不要緊,你並沒有真正執行這段代碼,因爲整個文件都被解析了。這與運行時錯誤不同,後者發生在代碼解析和代碼執行後。在你的情況下,錯誤發生在任何PHP代碼執行之前。

+0

thanx再次爲ur類支持 –