Insert Into不会插入Php / Mysqli

时间:2012-07-09 06:36:01

标签: php insert mysqli

我一直在尝试在我的数据库中向我的表中插入一行,我无法完成插入查询,我也无法得到任何错误,所以我不知道是什么问题

我的桌子:

CREATE TABLE IF NOT EXISTS `general_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `First Name` varchar(30) NOT NULL,
  `Last Name` varchar(30) NOT NULL,
  `University` varchar(80) NOT NULL,
  `Major` varchar(50) NOT NULL,
  `Current_Level` varchar(50) NOT NULL,
  `Date_Joined` varchar(15) NOT NULL,
  `Current_Team` varchar(50) NOT NULL,
  `added_by` int(11) NOT NULL,
  `Phone` varchar(25) NOT NULL,
  `Email` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `member_id` (`added_by`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

我的HTML:

<body>

        <div class="memberInfo"> 
        <h1>Add A Member</h1>

        <form id=add_member name="add_member" method="post" action="addmember.php" >

                <fieldset>

                <legend>Member Details</legend>

                <ol>

                    <li>
                        <label for=first_name>First Name</label>
                        <input id=first_name name=first_name type=text placeholder="First Name" required autofocus>
                    </li>

                    <li>
                        <label for=last_name>Last Name</label>
                        <input id=last_name name=last_name type=text placeholder="Last Name" required autofocus>
                    </li>

                    <li>
                        <label for=email>Email</label>
                        <input id=email name=email type=email placeholder="example@domain.com" required>
                    </li>

                    <li>
                        <label for=university>University</label>
                        <input id=university name=university type=text placeholder="Eg.Istanbul University" required>
                    </li>

                    <li>
                        <label for=major>Major</label>
                        <input id=major name=major type=text placeholder="Eg. Business" >
                    </li>

                    <li>
                        <label for=phone>Phone</label>
                        <input id=phone name=phone type=tel placeholder="Eg. +447500000000" >
                    </li>

                    <li>
                        <label for=current_stage>Current Stage</label>
                        <input id=current_stage name=current_stage type=text placeholder="Eg.Newbie" required>
                    </li>

                    <li>
                        <label for=current_team>Current Team</label>
                        <input id=current_team name=current_team type=text placeholder="Eg.External Relations">
                    </li>

                    <li>
                        <label for=date_joined>Date Joined</label>
                        <input id=date_joined name=date_joined type=text placeholder="Eg. 08/24/2012" required>
                    </li>
                    </ol>

            </fieldset>

            <fieldset>

                <button type=submit >Save Member</button>


            </fieldset>

        </form>
      </div>
    </body>

我如何在登录php中检索$ _SESSION ['user_id']

 if($stmt->num_rows==1){                    
        echo $myusername . " ";
        session_register("myusername");
        session_register("mypassword");

        $_SESSION['user_id']=$user_id;
        $_SESSION['username'] = $myusername;
        session_write_close();
        header("location:login_success.php");

    }

addmember.php:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    session_start();


if(isset( $_SESSION['user_id'])){


    $mysqli = new mysqli('localhost', 'root', 'password', 'aiesec');

    if(mysqli_connect_errno()) {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }


    $stmt = $mysqli -> prepare("INSERT INTO general_info (First Name, Last Name, University, Major, Current_Level, Date_Joined, Current_Team, added_by, Phone, Email) VALUES (?,?,?,?,?,?,?,?,?,?)") 

    $stmt -> bind_param("sssssssiss", $First_Name, $Last_Name,$University,$Major, $Current_Level, $Date_Joined, $Current_Team, $added_by, $Phone, $Email);

    $First_Name=$_POST['first_name'];
    $Last_Name=$_POST['last_name'];
    $University=$_POST['university'];
    $Major=$_POST['major'];
    $Current_Level=$_POST['current_stage'];
    $Date_Joined=$_POST['date_joined'];
    $Current_Team=$_POST['current_team'];

    $added_by=$_SESSION['user_id'];

    $Phone=$_POST['phone'];
    $Email=$_POST['email'];

    $stmt -> execute();

    printf("%d Row inserted.\n", $stmt->affected_rows);

    printf("Errormessage: %s\n", $stmt->error);

    $stmt->close();

    $mysqli->close();

}
else{

    printf("Nope\n"); 
    header("location:main_login.php");

}

    ?>

如果删除所有代码并将echo放到$_SESSION['user_id']输出为1,那么if语句是否有效。

我猜问题行可能是$added_by=$_SESSION['user_id'];,但我不确定。

如何修复插入或如何查看错误?

提前感谢任何建议。

-----编辑解决方案------- 有史以来最简单的错误

我已将First NameLast Name更改为First_NameLast_Name

我忘记在行尾添加分号 $stmt = $mysqli -> prepare("INSERT INTO general_info (First Name, Last Name, University, Major, Current_Level, Date_Joined, Current_Team, added_by, Phone, Email) VALUES (?,?,?,?,?,?,?,?,?,?)")

将分号脚本添加到数据库后

1 个答案:

答案 0 :(得分:2)

您的代码中存在错误

看看

$First Name, $Last Name

您需要在变量名称中加下下划线,如:

$First_Name, $Last_Name 

查看manual