按下按钮时无数据插入

时间:2015-09-21 09:18:24

标签: php mysql

当我按下提交按钮插入记录时,它没有出错,但是当我检查数据库时,我发现没有提交记录。请问我的剧本可能有什么问题。刚开始使用php

<?php

    if (isset($_POST['submitted'])){

        include('Connections/connect.php');

        $term= $_POST['term'];
        $details= $_POST['details'];



$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";



$newrecord ="Inserted Successfully";

    }

?>

connect.php

<?php

$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

HTML

<form id="form1" name="form1" method="post" action="page1.php">
  <p>
    <label for="term"></label>
    <input type="text" name="term" id="term" />
  </p>
  <p>
    <label for="details"></label>
    <input type="text" name="details" id="details" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Submit" />
     <input name="submitted" type="hidden" value="submitted"  />
  </p>
</form>
<p>
<?php
$newrecord
?>

6 个答案:

答案 0 :(得分:1)

您的代码存在很多问题 让我们一步一步地采取行动:

<?php

    if (isset($_POST['submitted'])){

        include('Connections/connect.php');

        $term= $_POST['term'];
        $details= $_POST['details'];

你没有逃到这里。当我成为一个坏人时,我可以摧毁你的申请 阅读更多关于逃离的信息:How can I prevent SQL injection in PHP?

$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";

您在此处定义查询,但不对此查询执行任何操作 阅读有关在php文档页面上执行查询的信息:http://php.net/manual/en/mysqli.query.php

$newrecord ="Inserted Successfully";

您在此处定义变量$newrecord,但此处没有函数。添加echo $newrecord;以回显变量$newrecord的值:http://php.net/echo

    }

?>

然后您没有在connect.php

中使用正确的变量
<?php

$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

您正在定义$hostname_speedapp并在$hostname_mydb中将mysqli_connect更改为$hostname_speedapp等。将您的连接字符串更改为: $mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp)

您没有在connectionstring中选择数据库。您正在使用名为$database_speedapp的数据库名称定义变量,但您从不使用它。

将你的连接字符串更改为:$mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp, $database_speedapp),你应该好好去

答案 1 :(得分:0)

使用它:

 ` $sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
   $result=mysqli_query($mydb,$sql);`

答案 2 :(得分:0)

您的脚本中甚至没有插入查询。

$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR);

$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";

if (mysqli_query($mydb, $sql)) {
    $newrecord ="Inserted Successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($mydb);
}

mysqli_close($mydb);

答案 3 :(得分:0)

添加此

    $sql = "INSERT INTO people (term,details) VALUES ($term,$details)";

    if (mysqli_query($mydb, $sql))
    {
        echo "New record created successfully";
    } 
    else
    {
        echo "Error: " . $sql . "<br>" . mysqli_error($mydb);
    }

编辑01

$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR); 

page1.php

的顶部
include("connect.php");

答案 4 :(得分:0)

Connect.php

  <?php
  $hostname_speedapp = "localhost";
  $database_speedapp = "mydb";
  $username_speedapp = "root";
  $password_speedapp = "password";
  $mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp,$database_speedapp) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

page1.php中     

if (isset($_POST['submit'])){

    include('Connections/connect.php');

    $term= $_POST['term'];
    $details= $_POST['details'];

    $sql = "INSERT INTO people (term,details) VALUES ('".$term."' , '".$details."')";
    if ($mydb->query($sql) === TRUE) {     //can use connected database $mydb 
             $newrecord = "New record created successfully";
    } else {
       echo "Error: " . $sql . "<br>" . $mydb->error;
    }

  }
 ?>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="term"></label>
<input type="text" name="term" id="term" />
</p>
<p>
<label for="details"></label>
<input type="text" name="details" id="details" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
<p>
<?php
if(isset($newrecord)){
echo "<h3>$newrecord</h3>";
}
?>

答案 5 :(得分:-2)

我认为你需要在sql查询中引用提交的数据值:

更改以下内容:

$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";

$sql = "INSERT INTO people (term,details) VALUES ('$term','$details')";