drpmazn9021 2012-07-09 06:36
浏览 60
已采纳

Insert Into不会插入Php / Mysqli

I have been trying to insert a row into my table in my database, I couldnt accomplished insert query also I cant get any errors so I dont know what is the problem

my table:

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 ;

my 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>

How i retrieve $_SESSION['user_id'] in my login php

 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.
", $stmt->affected_rows);

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

    $stmt->close();

    $mysqli->close();

}
else{

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

}

    ?>

If I remove all the code and put echo to see $_SESSION['user_id'] output is 1 so if statement works.

I guess problem line might be $added_by=$_SESSION['user_id']; but I am not sure.

How can I fix insert or How can I see the error?

Thanks in advance for any suggestions.

-----EDIT SOLUTION------- Simplest mistake of all times

I have changed First Name and Last Name to First_Name and Last_Name

I forgot to put semicolon at end of the line $stmt = $mysqli -> prepare("INSERT INTO general_info (First Name, Last Name, University, Major, Current_Level, Date_Joined, Current_Team, added_by, Phone, Email) VALUES (?,?,?,?,?,?,?,?,?,?)")

after putting semicolon script added the row to the database

  • 写回答

1条回答 默认 最新

  • dtdh11647 2012-07-09 06:40
    关注

    you have an errors in your code

    look on

    $First Name, $Last Name
    

    you need to have an underscore in variables name like:

    $First_Name, $Last_Name 
    

    look in the manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错