嘿朋友我是一個android dev & java devloper和第一次試圖在php代碼 我知道這個問題是在這裏問很多次,但我沒有從這個答案得到什麼是什麼完全錯誤。php插入代碼與使用mysql不工作
我的問題是,當我在這兩個字段中輸入值時,它不會插入到我的sql數據庫中。並且還記錄未插入的消息。
我需要當我點擊提交按鈕在那個時候數據去db和記錄插入消息來。
我認爲錯誤是在這裏 $connection = mysql_connect("localhost","000webhost", "id717479_kunal", "");
$db = mysql_select_db("id717479_info", $connection);
請告訴我什麼是這個代碼的任何幫助是值得歡迎的錯誤。
在這裏,我貼我的簡單的PHP插入代碼,請告訴我什麼是錯誤在此代碼
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Insert Data In Database Using PHP.</h2>
</div>
<form action="insert.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label>Enter Boy Name:</label>
<input class="input" name="boy" type="text" value=""><br><br>
<label>Enter Girl Name:</label>
<input class="input" name="girl" type="text" value=""><br><br>
<input class="submit" name="submit" type="submit" value="Insert">
</form>
</div>
</div>
</body>
</html>
<?php
$connection = mysql_connect("localhost","000webhost", "id717479_kunal", ""); // Establishing Connection with Server
$db = mysql_select_db("id717479_info", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$boy= $_POST['boy'];
$girl = $_POST['girl'];
if($boy!=''||$girl!=''){
//Insert Query of SQL
$query = mysql_query("insert into love(boy, girl) values ('$boy', '$girl')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysql_close($connection); // Closing Connection with Server
?>
使用'mysqli_connect',而不是'mysql_connect' –
@SahilGulati OP可能把它解釋爲「改變'mysql_connect'到'mysqli_connect'解決問題,而不會替換其他'mysql'的語法「 – Swellar
您使用的是棄用的函數 – Akintunde007