通过PHP将SQL Row插入PHPmyadmin

时间:2016-02-19 16:31:32

标签: 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),日期,时间,主页(varchar),离开(varchar),体育场(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>

Here is my form

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

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

如果我需要提供更多我能做的代码

由于

编辑小点我正在努力以网站所需的格式发布这个,所以如果我的块或代码或某些文字不整洁或者你有什么,我会事先道歉。

2 个答案:

答案 0 :(得分:1)

<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);
?>

答案 1 :(得分:0)

 <?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

关闭连接后你使用pre,看起来很奇怪

  

和mysqli_escape_string用于转义特殊字符