我需要創建一個數據庫,用戶輸入名字和姓氏,然後將其記錄到可按字母順序排序的表中。用戶創建的數據庫或表
我有什麼,我認爲是一個PHP表這裏的氣質:
<?php
$con=mysqli_connect("mysql6.000webhost.com","owen","*********","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql = "CREATE TABLE Persons (PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID), FirstName CHAR(15), LastName CHAR(15))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
接下來我有實際的HTML代碼在用戶提交他或她的名字:
<!DOCTYPE HTML>
<html>
<head>
<link rel="shortcut icon" href="http://2.bp.blogspot.com/-W6qoXhCjKSs/Tpul-XAR3-I/AAAAAAAACeU/Oe1ZEzpnUCg/s1600/sbstweb.gif">
<link rel="stylesheet" type="text/css" href="../style.css">
<title>Almost Done!</title>
</head>
<body>
<img class= "logo" src= "http://2.bp.blogspot.com/-W6qoXhCjKSs/Tpul-XAR3-I/AAAAAAAACeU/Oe1ZEzpnUCg/s1600/sbstweb.gif">
<h1>Enter your name below to complete the quiz</h1>
<form action="insert.php" method="post">
<p><input autofocus name="firstname" placeholder="First Name" type="text"></p>
<p><input autofocus name="lastname" placeholder="Last Name" type="text"></p>
<input type="submit">
</form>
</body>
</html>
最後一點我有我認爲是將代碼插入數據庫的php代碼:
<?php
$con=mysqli_connect("mysql6.000webhost.com","a3159217_owen","*********","a3159217_student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName)
VALUES
('$_POST[firstname]','$_POST[lastname]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
My fi第一個問題是如何讓php代碼實現和工作。當我在網頁上點擊提交時,它會將我指向我製作的insert.php代碼。我如何才能將它提交給數據庫,以及如何首先創建數據庫。
其次,爲了實際顯示錶格,是否必須製作另一個呈現php表格的html頁面?這個怎麼用?
1.把它放在運行php的web服務器上的文件中。你不應該編寫那麼多的代碼而無法運行它。 – 2013-05-29 03:39:09
所以我試過了,它說MySQL訪問被拒絕。它說我使用密碼YES,但我不是,而且在我的代碼中沒有。密碼應該放在MySQL連接提示符的哪個位置? –