<form name="contact_form" method="post" action="adminsqll.php">
S.No.: <input name="sno" type="number"><br>
Subject: <br><textarea name="subject" cols="60" rows="5" ></textarea><br>
Content: <br><textarea name="content" cols="60" rows="5" ></textarea><br>
<input type="submit" name="Submit" value="Submit">
</form>
这是我的sendtodb.php页面
<?php
// Create connection
$con=mysqli_connect('localhost','root','root');
if($con)
{
echo "connect";
}
/*
$sno =$_POST['sno'];
$subject = $_POST['subject'];
$content = $_POST['content'];
mysqli_select_db($con,'univ');
$sql="INSERT INTO news(Sno,subject,content,date) VALUES ($sno,'$subject','$content',CURRENT_DATE( ))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "inserted successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
答案 0 :(得分:0)
您必须更改mysqli_connect('localhost','root','root');
中的凭据并选择一个数据库(例如mysqli_select_db($link, "test");
,其中“test”是您的数据库名称。)
我建议您将数据库连接功能放在一个单独的文件中。您可能不仅仅需要在一个页面中连接到您的数据库,因此您不必再次调用mysqli_connect
。这样,如果您更改凭据,则无需在所有文件中更改凭据。例如:
<?php
// dbconnect.php
function dbconnect() {
$dbhost = "localhost";
$dbname = "test";
$dbuser = "username";
$dbpass = "password";
$cn = mysqli_connect($dbhost, $dbuser, $dbpass) or die('Connection error !');
mysqli_select_db($cn, $dbname) or die('Error while selecting database');
return $cn;
}
// sendtodb.php
include "/path/to/dbconnect.php";
$con = dbconnect();
if ($con) echo "connected !";
// your code...
?>