Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pdo/php在表中插入数据_Php_Mysql_Pdo - Fatal编程技术网

使用pdo/php在表中插入数据

使用pdo/php在表中插入数据,php,mysql,pdo,Php,Mysql,Pdo,我需要帮助,我的代码无法工作,无法插入表db。我觉得我的代码写得不好。我需要工作的部分是第一个开关盒 我需要它在pdo中,我的数据库和表数据是正确的 <?php if (isset($_GET['poslovni_korisnici'])) { $active1 = 'class="active"'; } else { $active1 = ''; } if(isset($_GET['korisnici'])) { $active2 = 'class="active"'; } else

我需要帮助,我的代码无法工作,无法插入表db。我觉得我的代码写得不好。我需要工作的部分是第一个开关盒

我需要它在pdo中,我的数据库和表数据是正确的

<?php

if (isset($_GET['poslovni_korisnici'])) {
$active1 = 'class="active"';
}
else {
$active1 = '';
}
if(isset($_GET['korisnici'])) {
$active2 = 'class="active"';
}
else {
$active2 = '';
}

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = '';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=zadatak1", $username, $password);
/*** echo a message saying we have connected ***/
/**echo 'Connected to database';**/
}
catch(PDOException $e)
{
echo $e->getMessage();
}

echo'
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Statistika</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- DataTables CSS -->
    <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">

    <!-- jQuery -->
    <script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

    <!-- DataTables -->
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesnt work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

    <nav class="navbar navbar-default">
    <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="index.php">POSLOVNA STATISTIKA</a>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
    <li ' . $active1 . '><a href="index.php?poslovni_korisnici">Poslovni korisnici <span class="sr-only">(current)</span></a></li>
    <li ' . $active2 . '><a href="index.php?korisnici">Korisnici</a></li>
    </ul>
    </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
    </nav>';

        switch (isset($_GET)) {
            case isset($_GET['unosPoslovni']):

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


                    $partner_name    = $_POST['Partner_name'] = NULL;
                    $partner_street  = $_POST['Partner_street'] = NULL;
                    $partner_zip     = $_POST['Partner_zip'] = NULL;
                    $partner_city    = $_POST['Partner_city'] = NULL;
                    $partner_country = $_POST['Partner_country'] = NULL;

                    $sql = "INSERT INTO poslovni_partneri 
                           (Partner_name,Partner_street, Partner_zip, Partner_city, Partner_country) 
                           VALUES('$partner_name','$partner_street',$partner_zip, $partner_city, $partner_country";                         
                    $result = mysql_query($sql);            
                  }         
                echo'
                <h1>Unos poslovni korisnika</h1>
                </br>
                </br>

                <form action="unos.php?unosPoslovni" method="POST" target="_parent" accept-charset="UTF-8">
                <table id="table_id" class="display">
                    <thead>
                        <tr>
                          <th>Partner name</th>
                          <th>Partner street</th>
                          <th>Partner zip</th>
                          <th>Partner city</th>
                          <th>Partner country</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="partner_name" value=""></td>
                            <td><input type="text" name="partner_street" value=""></td>
                            <td><input type="text" name="partner_zip" value=""></td>
                            <td><input type="text" name="partner_city" value=""></td>
                            <td><input type="text" name="partner_country" value=""></td>
                        </tr> 
                    </tbody>
                </table>        
                <input type="submit" name="submit1" value="Spremi" />
                </form>';



                break;

            default:
                # code...
                break;
        }

        switch (isset($_GET)) {
            case isset($_GET['unosKorisnici']):

                echo'
                <h1>Unos korisnika</h1>
                </br>
                </br>

                <form action="unos.php?unosPoslovni" method="POST" target="_parent" accept-charset="UTF-8">
                <table id="table_id" class="display">
                    <thead>
                        <tr>
                          <th>User name</th>
                          <th>User department</th>
                          <th>User email</th>
                          <th>User phone</th>
                          <th>User mobile</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="user_name" value=""></td>
                            <td><input type="text" name="user_department" value=""></td>
                            <td><input type="text" name="user_email" value=""></td>
                            <td><input type="text" name="user_phone" value=""></td>
                            <td><input type="text" name="user_mobile" value=""></td>
                        </tr> 
                    </tbody>
                </table>
                </form>';

                break;

            default:
                # code...
                break;
        }


    echo'
    <script>
      $(document).ready( function () {
          $("#table_id").DataTable();
      } );
    </script>

    <!-- jQuery (necessary for Bootstraps JavaScript plugins) -->
    <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>';    

?>

您的SQL查询无效,因为它缺少右括号(

应该更像这样:

INSERT INTO poslovni_partneri(Partner_name,Partner_street, Partner_zip, Partner_city,     Partner_country) 
VALUES('$partner_name','$partner_street','$partner_zip', '$partner_city', '$partner_country')";
你也应该和引语保持一致,你有时在变量周围使用引语,有时不使用引语

此外,你甚至不应该这样质疑;你应该使用,因为这种方式你是开放的

编辑

你说你需要PDO,你正在建立一个PDO连接,但是你正在使用
mysql.*
特定函数进行查询,这不是你应该如何使用PDO,与PDO无关

编辑2

您还应该验证post参数是否实际可用,对post参数执行以下操作:

$partner_name = isset($_POST['partner_name']) ? $_POST['partner_name'] : '';
注意小写的p

使用,并对所有post参数执行此操作,如果任何参数为空字符串,则显示错误而不是查询

编辑3


正如弗雷德所指出的,
合伙人的名字
!=
Partner_name
,即您的
name
属性标识符(在html中)与您试图在post参数中匹配的属性标识符不匹配

我看到了PDO,我看到了mysql,我看到了不带引号的值,缺少括号,还有什么可能出错?问你自己这个问题;-)我按下submit,但没有发生任何事情。您正在混合使用
mysql\uz
函数和PDO。只使用PDO。下面是您的答案。他肯定会知道的(我希望);-)我想这个问题会得到很多不同问题的答案。“把它们拼凑在一起”之类的东西。在这一点上,部分-OP必须把所有答案拼凑在一起,所以看起来是这样。@Fred ii-是的,不幸的是。就这样。。。通知:未定义索引:第94行C:\xampp\htdocs\senso\unos.php中的Partner\u名称通知:第95行C:\xampp\htdocs\senso\unos.php中的Partner\u街道未定义索引:第96行C:\xampp\htdocs\senso\unos.php中的Partner\u zip通知:第97行未定义索引:C:\xampp\htdocs\senso\unos.php中的Partner\u城市通知:未定义索引:第98行C:\xampp\htdocs\senso\unos.php中的Partner\u country^simple;这是一个书箱问题<代码>A!=a
另外,你在
$\u POST['Partner\u name'
中也缺少一个括号,这不是正确的“case”。我本想给出一个答案,但这个问题有太多的问题。
$partner_name = isset($_POST['partner_name']) ? $_POST['partner_name'] : '';