php - 通过 PHP 将 SQL 行插入 PHPmyadmin

标签 php mysql mysqli

您好,我正在努力向我的表中添加一行,但无法弄清楚为什么会出现这些错误。

Warning: mysqli_connect() expects parameter 5 to be long, string given in   C:\xampp\htdocs\loginSystem\insert.php on line 4

Notice: Undefined index: in C:\xampp\htdocs\loginSystem\insert.php on line 13

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\loginSystem\insert.php on line 13

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\loginSystem\insert.php on line 14

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\loginSystem\insert.php on line 15

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null     given in C:\xampp\htdocs\loginSystem\insert.php on line 16

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null     given in C:\xampp\htdocs\loginSystem\insert.php on line 17

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null     given in C:\xampp\htdocs\loginSystem\insert.php on line 18

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in     C:\xampp\htdocs\loginSystem\insert.php on line 22

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in     C:\xampp\htdocs\loginSystem\insert.php on line 25
ERROR: Could not able to execute INSERT INTO fixtures (ID,Date, Time, Home,     Away, Stadium) VALUES ('','', '', '', '', ''). 
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in     C:\xampp\htdocs\loginSystem\insert.php on line 29

我的表格如下—— ID(主键autoinc), Date, Time, Home(varchar), Away(varchar), Stadium(varchar)

这是我的数据库连接

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", " ", "mancity", "fixtures");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security

$ID = mysqli_real_escape_string($link, $_POST['']);
$Date = mysqli_real_escape_string($link, $_POST['Date']);
$Time = mysqli_real_escape_string($link, $_POST['Time']);
$Home = mysqli_real_escape_string($link, $_POST['Home']);
$Away = mysqli_real_escape_string($link, $_POST['Away']);
$Stadium = mysqli_real_escape_string($link, $_POST['Stadium']);

// attempt insert query execution
$sql = "INSERT INTO fixtures (ID, Date, Time, Home, Away, Stadium) VALUES     ('$ID','$Date', '$Time', '$Home', '$Away', '$Stadium')";
if(mysqli_query($link, $sql)){
    echo "Fixture Added.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
</pre>

这是我的表格

<form action="insert.php" method="post"> <p> <label for="Date">Date:</label> <input type="date" name="Date" id="Date"> </p> <p> <label for="Time">Time:</label> <input type="time" name="Time" id="Time"> </p> <p> <label for="Home">Home Team:</label> <input type="text" name="Home" id="Home"> </p> <p> <label for="Away">Away Team:</label> <input type="text" name="Away" id="Away"> </p> <p> <label for="Stadium">Venue:</label> <input type="text" name="Stadium" id="Stadium"> </p> <input type="submit" value="Submit"> </form>

据我了解,给定数据的中间 block 应该接受来自表单的信息,然后将其添加到行中。但正如我所说,我在帖子顶部得到了错误。

如果我需要提供更多代码我可以做

谢谢

编辑 小问题 我一直在努力以网站要求的格式发布此内容,因此如果我的 block 或代码或某些文本不整洁或您有什么问题,我提前道歉。

最佳答案

<form action="insert.php" method="post">
  <p>
      <label for="Date">Date:</label>
      <input type="date" name="Date" id="Date">
  </p>
  <p>
      <label for="Time">Time:</label>
      <input type="time" name="Time" id="Time">
  </p>  
  <p>
      <label for="Home">Home Team:</label>
      <input type="text" name="Home" id="Home">
  </p>
  <p>
      <label for="Away">Away Team:</label>
      <input type="text" name="Away" id="Away">
  </p>
  <p>
      <label for="Stadium">Venue:</label>
      <input type="text" name="Stadium" id="Stadium">
  </p>
  <input type="submit" value="Submit">
</form>

insert.php

我希望 mancity 是您的数据库名称。

<?php

$link = mysqli_connect("localhost", "root", "", "mancity");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security

$Date = mysqli_real_escape_string($link, $_POST['Date']);
$Time = mysqli_real_escape_string($link, $_POST['Time']);
$Home = mysqli_real_escape_string($link, $_POST['Home']);
$Away = mysqli_real_escape_string($link, $_POST['Away']);
$Stadium = mysqli_real_escape_string($link, $_POST['Stadium']);

// attempt insert query execution
$sql = "INSERT INTO `fixtures`(`Date`, `Time`, `Home`, `Away`, `Stadium`) VALUES('$Date', '$Time', '$Home', '$Away', '$Stadium')";

if(mysqli_query($link, $sql)){
    echo "Fixture Added.";
} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

关于php - 通过 PHP 将 SQL 行插入 PHPmyadmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510152/

相关文章:

PHP/MYSQL错误: Object of class mysqli_result could not be converted to string

php - 当我尝试运行 Yii 服务时,它什么也不做。 (fsockopen(): unable to connect)

php - October CMS - 将内容与主题分开

c# mysql incrementing null 错误

mysql - 如何在mysql中索引用户名

php - MySQL 出现重复更新错误

php - 如何使用oops php5概念将数据插入mysql数据库?

phpass 的自定义 base 64 编码器 : does it have a name/advantage over Base64?

mysql - 如何使用 LOAD DATA INFILE 将 CSV 文件中的选定列插入 MySQL 数据库

php - 如何获得距离当前日期 3(不同)天(差异)的结果?