我在遠程服務器上創建了兩個文件。一個是html表單,它要求輸入一些字段,另一個是一個php文件,它將獲取所有數據並插入到數據庫中。將HTML表單提交到PHP文件
對於HTML格式的文件,點擊提交按鈕我正在調用php文件,但文件沒有得到執行我想因爲當我點擊提交它再次加載相同的html頁面。
HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MCQ Questions</title>
</head>
<body>
<form method="post" >
<p> Enter the question :</p> <input name="question" type="input"> <br><br>
<p> Enter options :</p>
Enter option 1 : <input name="opt1" type="input"> <br><br>
Enter option 2 : <input name="opt2" type="input"> <br><br>
Enter option 3 : <input name="opt3" type="input"> <br><br>
Enter option 4 : <input name="opt4" type="input"> <br><br>
<p> Enter correct answer :</p>
<input name="ans" type="input"> <br><br>
<input type="submit" value = "Submit" onClick = "uploadQuestion.php">
</form>
</body>
</html>
php文件:
<?php
$question=$_POST['question'];
$option1=$_POST['opt1'];
$option2=$_POST['opt2'];
$option3=$_POST['opt3'];
$option4=$_POST['opt4'];
$ans=$_POST['ans'];
$db_server = mysql_connect("address","username","pass");
if(!$db_server) {
die("Database connection failed: " . mysql_error());
}else{
$db_select = mysql_select_db("mlm",$db_server);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}
$sql = "INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer) VALUES ('$question','$option1',$option2,$option3,$option4,$ans)";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
?>
我也試過這樣:
<input type="submit" value = "Submit" onClick = "http://address/uploadQuestion.php">
但沒有什麼工作。這裏有什麼問題?我是網頁開發的初學者,任何人都可以幫忙嗎?謝謝。
編輯:
$database = new Database('addredd','username','pass','handbook');
$dbConnection = $database->getDB();
$stmt = $dbConnection->prepare("insert into questions(question,answer_a,answer_b,answer_c,answer_d,answer) values(?,?,?,?,?,?)");
$stmt->execute(array($question,$option1,$option2,$option3,$option4,$ans));
我試圖用PDO語句,但得到這個錯誤: 致命錯誤:類「數據庫」中/var/www/html/uploadQuestion.php未發現線12
EDIT2:
我想上傳服務器上一個文件並希望將其保存在數據庫中還,所以爲了這個,我已經創造了2個文件一個是index.php文件,另一個是uploadFile.php。
正如你現在所示,我使用了pdo,但是當我再次單擊上傳圖片時,同一頁面正在加載。
的index.php
<form action="index.php" method="post" enctype="multipart/form-data">
<p> Select image to upload:</p>
<input name = "file" type="file" id="fileToUpload"><br><br>
Enter chapter name :
<input name = "chapterName" type = "text"><br><br>
<input type="submit" value = "Upload Image">
</form>
<?php
if (isset($_FILES['file']['tmp_name']))
{
$ch = curl_init();
$cfile = new CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']);
$data = array("myfile" => $cfile);
curl_setopt($ch, CURLOPT_URL, "http://host/NewProject/uploadFile.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOTP_POSTFIELDS, $data);
$response = curl_exec($ch);
if($response == true)
{
echo "File posted";
}
else{
echo "Error: " . curl_error($ch);
}
}
?>
uploadFile.php
<?php
ini_set('display_errors', 1);
if(isset($_FILES['myfile']['tmp_name']))
{
$path = "files/" . $_FILES['myfile']['name'];
move_uploaded_file($_FILES['myfile']['tmp_name'], $path);
$chapterName=$_POST['chapterName'];
$dbh = new PDO('mysql:host=host;dbname=database_name','username', 'password');
$stmt = $dbh->prepare("INSERT INTO chapters (title,file) VALUES (?, ?)");
$stmt->execute(array($chapterName,$path));
if ($dbh->lastInsertId())
{
echo 'File upploaded.';
}
else
{
echo 'File could not upload.';
}
}
?>
請幫助..謝謝..
這個按鈕,刪除'的onClick = 「uploadQuestion.php」'和'添加行動=「uploadQuestion.php 「'在開幕'
[**請不要在新代碼中使用'mysql_ *'函數**](http://stackoverflow.com/q/12859942)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。 請參閱[**紅框**](http://php.net/mysql-connect)? 改爲了解[*預準備語句*](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://php.net/pdo)或[MySQLi](http:///php.net/mysqli) - [這篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)可以幫助你決定哪個。 – Qirel
'type =「input」'不是有效的類型。 https://developer.mozilla.org/en/docs/Web/HTML/Element/input –