2017-03-18 114 views
0

我在远程服务器上创建了两个文件。一个是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.'; 
} 


} 
    ?> 

请帮助..谢谢..

+2

这个按钮,删除'的onClick = “uploadQuestion.php”'和'添加行动=“uploadQuestion.php “'在开幕'

'标签内。 [** PHP手册**](http://php.net/manual/en/tutorial.forms.php)也有关于如何处理表单的例子,你应该从那里开始。 – Qirel

+0

[**请不要在新代码中使用'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

+0

'type =“input”'不是有效的类型。 https://developer.mozilla.org/en/docs/Web/HTML/Element/input –

回答

1

首先修复你的形式,type=""不能被命名inputü可以在这里查看https://www.w3schools.com/tags/att_input_type.asp

<!doctype html> 
<html> 

<head> 
    <meta charset="utf-8"> 
    <title>MCQ Questions</title> 
</head> 

<body> 

    <form action="uploadQuestion.php" method="post" enctype="multipart/form-data"> 

    <p> Enter the question :</p> <input name="question" type="text"> <br><br> 

    <p> Enter options :</p> 
    Enter option 1 : <input name="opt1" type="text"> <br><br> 
    Enter option 2 : <input name="opt2" type="text"> <br><br> 
    Enter option 3 : <input name="opt3" type="text"> <br><br> 
    Enter option 4 : <input name="opt4" type="text"> <br><br> 

    <p> Enter correct answer :</p> 

    <input name="ans" type="text"> <br><br> 

    <input type="submit" value="Submit"> 

    </form> 
</body> 

</html> 

然后你php代码

<?php 

// mysql connection 
$db_server = mysql_connect("address","username","pass"); 

// check for mysql connection 
if(!$db_server) 
{ 
    die("Database connection failed: " . mysql_error()); 
} 
else 
{ 
    // check if database exists 
    $db_select = mysql_select_db("mlm",$db_server); 

    if (!$db_select) 
    { 
     die("Database selection failed:: " . mysql_error()); 
    } 
} 

// escape post variables 
$question = mysql_real_escape_string($_POST['question']); 
$option1 = mysql_real_escape_string($_POST['opt1']); 
$option2 = mysql_real_escape_string($_POST['opt2']); 
$option3 = mysql_real_escape_string($_POST['opt3']); 
$option4 = mysql_real_escape_string($_POST['opt4']); 
$ans = mysql_real_escape_string($_POST['ans']); 




// make query 
$sql = "INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer) VALUES ('$question', '$option1', '$option2', '$option3', '$option4', '$ans')"; 

// check if query runs  
if (!mysql_query($sql)) { 
    die('Error: ' . mysql_error()); 
} 

?> 

或者phpmysqli

<?php 

// host, username, password, database name 
$db_server = mysqli_connect("address", "username", "pass", "mlm"); 

// check for connection 
if(!$db_server) 
{ 
    die("Database connection failed: " . mysqli_error($db_server)); 
} 

// escape post variables 
$question = mysqli_real_escape_string($db_server, $_POST['question']); 
$option1 = mysqli_real_escape_string($db_server, $_POST['opt1']); 
$option2 = mysqli_real_escape_string($db_server, $_POST['opt2']); 
$option3 = mysqli_real_escape_string($db_server, $_POST['opt3']); 
$option4 = mysqli_real_escape_string($db_server, $_POST['opt4']); 
$ans = mysqli_real_escape_string($db_server, $_POST['ans']); 

// make query 
$sql = "INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer) VALUES ('$question', '$option1', '$option2', '$option3', '$option4', '$ans')"; 

// check if query runs 
if (!mysqli_query($db_server, $sql)) 
{ 
    die('Error: ' . mysqli_error($db_server)); 
} 

?> 

或者php预处理语句

<?php 

// mysql connection 
$dbh = new PDO('mysql:host=adress;dbname=database_name', 'username', 'password'); 

// escape post variables 
$question = $_POST['question']; 
$option1 = $_POST['opt1']; 
$option2 = $_POST['opt2']; 
$option3 = $_POST['opt3']; 
$option4 = $_POST['opt4']; 
$ans = $_POST['ans']; 


$stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer) VALUES (?, ?, ?, ?, ?, ?)"); 
$stmt->execute(array($question, $option1, $option2, $option3, $option4, $ans)); 

if ($dbh->lastInsertId()) 
{ 
    echo 'Sucess.'; 
} 
else 
{ 
    echo 'Fail.'; 
} 

?> 
+0

我得到这个错误:mysqli_connect():标题和客户端库次要版本不匹配。标题:50550库:50631在/var/www/html/uploadQuestion.php 34行 – Sid

+0

你在本地主机上编码,xampp? – Mario

+0

其远程服务器 – Sid

-2

更改您的代码从这个

<!doctype html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>MCQ Questions</title> 
 
</head> 
 

 
<body> 
 

 
    <form action="uploadQuestion.php" method="post" enctype="multipart/form-data"> 
 

 
    <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"> 
 

 
    </form> 
 
</body> 
 

 
</html>