dousonghs58612 2016-09-04 23:02
浏览 49

表单:INSERT到数据库,不工作

I cannot seem to make this code work. It is also a base for my other page(update) that doesn't want to update.

The code is supposed to add information into the database.

The query works by itself. I thought that maybe query is too complex(because pro has fields that are connected to different tables: fundpro, city, typepro and city is connected to country), but since it works by itself I guess there is a way to make it work in PHP and I'm just doing it wrong.

In update page(link above to question and the code) form shows correct fields, so I assume there are no major mistakes in both pages, the problem is in the "delivery" of the information to the database or something.

I really need the insert-data page, it is practically the only page i really need in the end, and I cannot make it work to my org and pro tables, while my country tables has workable add and edit pages that is quite similar.

I ran out of ideas what might be the reason and I do not know how to make it work for me. Maybe someone sees the problem or knows an alternative approach?

tl;dr data doesn't get INSERTed into the database from the fields. query is coccer by itself, based on update.php file, form returns the information correctly.

Here are SQLFiddle and EER Diagram.

    <?php 
    header('Content-type: text/html; charset=utf-8');


    isset($_GET['submit'])?$submit=true:$submit=false;

    if(!$submit){
        ?>
        <!DOCTYPE html>
    <html>
    <head>
    <style>h1{color:red;}label{color:darkred;}</style>
    <title>Add project</title>
    <meta charset=”UTF-8”>      

    </head>

    <body>
    <h1>Add project:</h1>
    <form name="f1" action="" method="GET" onSubmit="return validateForm(this)">


<label>IDpro </label><input type="text" name="idpro"><br>
<label>numpro</label><input type="text" name="numpro" ><br>
<label>namepro </label><input type="text" name="namepro" ><br>
<label>datef </label><input type="text" name="datef" ><br>
<label>datet </label><input type="text" name="datet" ><br>
<label>descr </label><input type="text" name="descr" ><br>
<label>typepro </label><input type="text" name="typepro" ><br>
<label>fundpro </label><input type="text" name="fundpro" ><br>
<label>IDci </label><input type="text" name="idpro" ><br>


    <input type="reset" value="clear">
    <input type="submit" value="add" name="submit"><br>
    </form>
    </body>
    </html>
    <?php
    }else{
        include "../config.php";
    isset($_GET['idpro']) ? $idpro=$_GET['idpro'] : $idpro='';
    isset($_GET['numpro']) ? $numpro=$_GET['numpro'] : $numpro='';
    isset($_GET['namepro']) ? $namepro=$_GET['namepro'] : $namepro='';
    isset($_GET['datef']) ? $datef=$_GET['datef'] : $datef='';
    isset($_GET['datet']) ? $datet=$_GET['datet'] : $datet='';
    isset($_GET['descr']) ? $descr=$_GET['descr'] : $descr='';
    isset($_GET['typepro']) ? $typepro=$_GET['typepro'] : $typepro='';
    isset($_GET['fundpro']) ? $fundpro=$_GET['fundpro'] : $fundpro='';
    isset($_GET['idci']) ? $idci=$_GET['idci'] : $idci='';

        $sql="INSERT INTO pro(idpro, numpro, namepro, datef, datet, descr,typepro_idtypepro, fundpro_idfundpro, city_idci) VALUES (?,?,?,?,?,?,?,?,?)";

        $stmt = $mysqli->prepare($sql);

        $stmt->bind_param('ssddsiiii', $numpro, $namepro, $datef, $datet,$descr, $typepro_idtypepro, $fundpro_idfundpro, $city_idci, $idpro);

        $stmt->execute();

        $stmt->close();

        $mysqli->close();

        header("Location: view.php");
        //}
    }
    ?>

UPDATE: simpler code, same pattern:

header('Content-type: text/html; charset=utf-8');


isset($_GET['submit'])?$submit=true:$submit=false;

if(!$submit){
    ?>
    <!DOCTYPE html>
<html>
<head>
<style>h1{color:red;}label{color:darkred;}</style>
<title>Add city</title>
<meta charset=”UTF-8”>      

</head>

<body>

<h1>Add city:</h1>
<form name="f1" action="" method="GET" onSubmit="return validateForm(this)" >
<label>city id </label><input type="text" name="idci"><br>
<label>city </label><input type="text" name="nameci"><br>

<label>country id </label><input type="text" name="country_idco"><br>
<input type="reset" value="clear">
<input type="submit" value="add" name="submit"><br>


</body>
</html>
<?php
}else{
    include "../config.php";
    isset($_GET['idci'])?$idci=$_GET['idci']:$idci='';
    isset($_GET['nameci'])?$nameci=$_GET['nameci']:$nameci='';
//  isset($_GET['nameco'])?$nameco=$_GET['nameco']:$nameco='';
    isset($_GET['idco'])?$idco=$_GET['idco']:$idco='';

    //if(($name!='')&&($nameci!='')&&($nameco!='')){
        //echo "$name $nameci $nameco";


    $sql="INSERT INTO city(idci, nameci,country_idco) VALUES (?,?,?)";

    $stmt = $mysqli->prepare($sql);

    $stmt->bind_param('isi', $idci, $nameci, $country_idco);

    $stmt->execute();

    $stmt->close();

    $mysqli->close();

    header("Location: view.php");
    //}
}
  • 写回答

2条回答 默认 最新

  • douhu3424 2016-09-04 23:05
    关注

    You are only allowing for 9 parameters in your insert statement but are passing through 10 in your bind_param method.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)