简体   繁体   English

为什么这不会插入表格?

[英]Why this won't insert into table?

Here is my code for adding users to mysql database, I really don't know why it won't submit it... 这是我将用户添加到mysql数据库的代码,我真的不知道它为什么不提交它...

    <?php include 'includes/header.php'; ?>
<?php 
    if (isset($_post['submit'])) {
        $ime_priimek = $_post['ime_priimek'];
        $spol = $_post['spol'];
        $razred = $_post['razred'];
        $slika = $_post['slika'];
        $sql = mysqli_query($con, "INSERT INTO uporabniki (ime_priimek, spol, razred, slika) VALUES ('". $ime_priimek ."' , '". $spol ."' , '". $razred ."' , '". $slika ."' , )" or die(mysql_error()));
        echo "Dodan";
    } else {
        echo "Ni dodan";
    }
 ?>
<div class="container main">
    <div class="row">
        <div class="col-lg-8">
            <div class="panel panel-primary">
              <div class="panel-heading">Dodaj uporabnika</div>
              <div class="panel-body">
                <form role="form" action="admin.php?g=miha" method="post" accept-charset="utf-8">
                  <div class="form-group">
                    <label for="ime_priimek">Ime in priimek:</label>
                    <input type="text" name="ime_priimek" class="form-control" id="ime_priimek" placeholder="Andrej Novak">
                  </div>
                  <div class="form-group">
                    <label for="spol">Spol:</label>
                    <input type="text" name="spol" class="form-control" id="spol" placeholder="Moški">
                  </div>
                  <div class="form-group">
                    <label for="razred">Razred:</label>
                    <input type="text" name="razred" class="form-control" id="razred" placeholder="2. Mb">
                  </div>
                  <div class="form-group">
                    <label for="slika">Slika:</label>
                    <input type="text" name="slika" class="form-control" id="slika" placeholder="https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash3/1391614_10200902760433385_105742999_n.jpg">
                  </div>
                  <button class="btn btn-primary" name="submit" type="submit">Dodaj</button>
                </form>
              </div>
            </div>
        </div>

I can't find reason why this code won't insert data in my database. 我找不到为什么这段代码不会在我的数据库中插入数据的原因。 Please help! 请帮忙!

For one thing, $_POST is a superglobal and must be in uppercase like this $_POST 首先, $_POST是一个超全局 ,必须像$_POST一样大写

yours are all in lowercase $_post 你的都是小写的$_post

I also noticed there are no DB credentials for $con in your posted code. 我还注意到您发布的代码中没有$con数据库凭据。

You need to be doing something to the affect of: 你需要做一些事情来影响:

$con=mysqli_connect("xxx_host", "xxx_user", "xxx_password");
mysqli_query($con, "INSERT INTO uporabniki (ime_priimek, spol, razred, slika) VALUES 
('". $ime_priimek ."' , '". $spol ."' , '". $razred ."' , '". $slika ."' )" or die(mysql_error()));

and you had a stray comma in '". $slika ."' , )" 你在'". $slika ."' , )"有一个迷路逗号'". $slika ."' , )"


You can also try this method: 您也可以尝试这种方法:

$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';

$con= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 

$sql = $con->prepare("INSERT INTO uporabniki (ime_priimek, spol, razred, slika) VALUES (?, ? ,? ,?)");
$sql->bindParam(1, $ime_priimek);
$sql->bindParam(2, $spol);
$sql->bindParam(3, $razred);
$sql->bindParam(4, $slika);
$sql->execute();

There are several mistakes in your code. 您的代码中存在多个错误。

$_post ? $_post Unless you declared it yourself, PHP only has $_POST . 除非你自己声明,否则PHP只有$_POST Variable names are case-sensitive . 变量名称区分大小写

I suppose thats basically why your insert won't work, howover, there is more. 我想这就是为什么你的插入不起作用,然而,还有更多。

You are getting values sent from the client and trowing them, as is, directly inside a SQL Query. 您将获取从客户端发送的值,并将其直接放入SQL查询中。 Bad move. 不好的举动。 Perhaps you want to learn the principles of SQL Injection and how to prevent it. 也许您想学习SQL注入的原理以及如何防止它。

Also, I don't see how mysql_error() will help you here, sinse you are using MySQLi. 另外,我没有看到mysql_error()将如何帮助你,你使用MySQLi。 Try mysqli_error() instead. 请尝试使用mysqli_error()

You need to change $_post to uppercase. 您需要将$ _post更改为大写。 Like: 喜欢:

<?php 
    if (isset($_POST['submit'])) {
        $ime_priimek = $_POST['ime_priimek'];
        $spol = $_POST['spol'];
        $razred = $_POST['razred'];
        $slika = $_POST['slika'];
        $sql = mysqli_query($con, "INSERT INTO uporabniki (ime_priimek, spol, razred, slika) VALUES ('". $ime_priimek ."' , '". $spol ."' , '". $razred ."' , '". $slika ."' , )" or die(mysql_error()));
        echo "Dodan";
    } else {
        echo "Ni dodan";
    }
 ?>

Everytime it is good to format your queries. 每次格式化查询都很好。 If you have formated yours you should see that you have one extra , at the end 如果你已经形成了你的结构,你应该看到最后有一个额外的

$query = "INSERT INTO uporabniki (ime_priimek, spol, razred, slika)";
$query .= " VALUES ('". $ime_priimek ."','". $spol ."','". $razred ."','". $slika ."')";
$sql = mysqli_query($con, $query) OR die("ERROR: ".mysqli_error());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM