Closed. This question is off-topic。它目前不接受答案。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
四年前关闭。
既然我是个新手,我想问问你的问题。即使没有出现错误消息,插入查询似乎也无法正常工作。
代码如下:
<?php
include "../../inc/inc.koneksi.php";

$table      = "admins";

$user       = str_replace("'","\'",$_POST['user']);
$pwd        = md5($_POST['pwd1']);
$nama       = str_replace("'","\'",$_POST['nama']);
$level      = $_POST['level'];
$blokir     = $_POST['blokir'];

$sql = mysql_query("SELECT username, password, nama_lengkap, level, blokir
                   FROM $table
                   WHERE username= '$user'");
$row    = mysql_num_rows($sql);
if ($row > 0){
    $input  = "UPDATE $table SET password       ='$pwd',
                                nama_lengkap    ='$nama',
                                level   ='$level',
                                blokir  ='$blokir',
                                lastupdate  = now()
                    WHERE username= '$user'";

    // if(!mysql_query($input));
    if ( mysql_query($input) ) {
        echo "Record updated successfully.";
    } else {
        echo "Something went wrong with the query.";
    }


} else {
    $input = "INSERT INTO $table (username, password, nama_lengkap, level, blokir, create)
                VALUES ('".$user."', '".$pwd."', '".$nama."', '".$level."', '".$blokir."', now() )";

    //if (!mysql_query($input));
    if ( mysql_query($input) ) {
        echo "New record created successfully.";
    } else {
        echo "Something went wrong with the query.";
    }
}
echo "<br>".$input."<br/>";
?>

真的需要你的帮助:)

最佳答案

mysql_query已弃用,不应用于新代码。使用PDO进行数据库访问,并从数据库连接/语句处理程序获取错误消息。
下面的示例向您展示如何设置PDO,如何执行准备好的语句,以及如果有错误消息,如何获取错误消息。请注意,我们不是将这些值连接到字符串中,而是将它们作为命名参数传递。当你这样做的时候,PDO会负责消毒和逃跑。我还删除了表名的变量——只需在查询中键入它。

// This is how to establish a PDO connection
$host="[replace me]"; // Host name
$username="[replace me]"; // username
$password="[replace me]"; // password
$db_name="[replace me]"; // Database name

/*
  you do this once, then pass $pdo into class constructors or as a function argument
  when db access is needed
  do not use the global keyword. don't do it.
*/
$pdo = new PDO('mysql:host='.$host.';dbname='.$db_name, $username, $password);


/*
  before you do your query, you should validate the input. Make sure all the
 fields exist, and have valid data. You should not assume they're there and valid,
 even if you validated the form with javascript. This is one of many ways to do it.
*/

$errors = array();
foreach (
    array(
        'username',
        'pwd1',
        'nama',
        'level',
        'blokir'
    ) as $field
){
    if (
        array_key_exists($field, $_POST) === false ||
        strlen(trim($_POST[$field])) < 1
    ){
        $errors[] = 'Missing required field: '.$field;
    }
}

// only do the query if you have all the values!
$result = false;
if (count($errors) === 0) {
    // make a prepared statement
    $pdoStatement= $pdo->prepare('
        INSERT INTO admins (
            username,
            password,
            nama_lengkap,
            level,
            blokir,
            create
        ) VALUES (
            :username,
            :password,
            :nama_lengkap,
            :level,
            :blokir,
            NOW()
        )
    ');
    $result = $pdoStatement->execute(array(
       'username'=>$_POST['user'],
       'password'=>$_POST['pwd1'],
       'nama_lengkap'=>$_POST['nama'],
       'level'=>$_POST['level'],
       'blokir'=>$_POST['blokir']
    ));
    if (!$result) $errors = $pdoStatement->errorInfo();
}
if (!$result){
    // do something better with the error messages!
    echo 'There were errors: <ul><li>'.implode('</li><li>', $errors).'</li></ul>';
}

文档
PDO-http://php.net/manual/en/book.pdo.php
PDO::prepare-http://php.net/manual/en/pdo.prepare.php
PDOStatement-http://php.net/manual/en/class.pdostatement.php
PDOStatement::execute-http://php.net/manual/en/pdostatement.execute.php
PDOStatement::errorInfo-http://php.net/manual/en/pdostatement.errorinfo.php

关于php - 插入查询MySQL不起作用,尽管没有错误消息或警告。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27223292/

10-12 12:42
查看更多