簡體   English   中英

數據庫和PHP未連接到phpMyAdmin(基本代碼)

[英]Database and PHP not connecting to phpMyAdmin (basic code)

index.html的:

<!DOCTYPE html>
<html lang="en"> 
<head>

<body>

<form id="myform" action="userinfo.php" method="post" >

Name:   <input type="test" name="name"  autofocus>

Age:    <input type="text" name="age"  >
    <button id="sub"> save</button>

 </form>

 <span id="result"></span>

  <script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
 <script src="script/my_script.js" type="text/javascript"></script>
 </body>
 </html>

my_script.js:

$("#sub").click( function() {
$.post( $("#myform").attr("action"), 
     $("#myform :input").serializeArray(), 
     function(info){ $("#result").html(info); 
});
clearInput();
});

$("#myform").submit( function() {
  return false; 
  });

function clearInput() {
$("#myform :input").each( function() {
   $(this).val('');
});

db.php中:

<?php

$conn = mysql_connect('localhost','B00556019','73eKESV3') ; 
$db = mysql_select_db('b00556019');

?>

userinfo.php:

<?php

    include_once('db.php');

    $name = $_POST['name'];
    $age = $_POST['age'];

    if(mysql_query("INSERT INTO user (name, age) VALUES (
                   '$name','$age')";))
        echo "Successful";
    else
        echo "insertion failed";

?>

它不會發布到數據庫

數據庫名稱:b00556019
表:用戶
欄位:name(varchar,15)。 年齡(INT,3)

除了userinfo頁顯示為空白以外,沒有任何反應。

任何對如何發布到數據庫有任何建議的人都很好,並且如果有人有簡單的教程,因為這是基本的教程,但仍然行不通。

旁注:就目前而言,您可以進行SQL injection

請考慮切換到使用mysqli_*函數以及已准備好的語句或PDO。 mysql_*函數已被棄用,並將在以后的PHP版本中刪除。

代替:

if(mysql_query("INSERT INTO user (name, age) VALUES (
               '$name','$age')";))
    echo "Successful";
else
    echo "insertion failed";

采用:

$name = mysql_real_escape_string($_POST['name']);
$age = mysql_real_escape_string($_POST['age']);

$result = mysql_query("INSERT INTO user (name, age) VALUES ('$name','$age')");
    if(!mysql_query($result)){ 
        echo "Insertion was not successful."; 
} else {
    echo "Insertion was successful.";
}

您可能還需要通過數據庫連接:

if(!mysql_query($result,$conn))

准備報表的方法:

$conn = new mysqli("xxx", "xxx", "xxx", "xxx");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$name = $_POST['name'];
$age = $_POST['age'];

$result = $conn->prepare("INSERT INTO user (name, age) VALUES (?,?)");
$result->bind_param("ss", $name, $age);
$result->execute();
$result->close();

// If there is any error with your SQL statement an
// error is thrown and displayed to the user:
printf("Prepared Statement Error: %s\n", $conn->error);

首先更正表名稱:“ table:users”,它是您在代碼中使用的“ user”。

if(!$conn){
     echo mysql_error() ; // first check while connecting..
}

// on the insertion code ..... 

if(mysql_query("INSERT INTO user (name, age) VALUES (
               '$name','$age')";))
    echo "Successful";
else
    echo mysql_error($conn) ; // you will see what is the error..
    echo "insertion failed";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM