调用数据库函数不返回

时间:2018-01-22 23:06:27

标签: php html mysql sql

以下代码用于在测验开始时创建用户,但现在正在更新现有用户的信息。对addUser()的调用永远不会完成,我不确定为什么我只是更改了它重定向到的页面。所有输入信息都是准确的,可用于我的databaseAdaptor类中的addUser()调用,因此我不确定我遇到了什么问题。换句话说,页面末尾的表单永远不会被提交,所以我永远不会转到final.php

$_SESSION['array_original'];
if(isset($_POST["user"]) ){
    $user = $_POST["user"];
    $array = $_SESSION['array_original'];
}
$height_in = $_POST['in'];
$height_ft = $_POST['ft'];
$height = 2.54 * (($height_ft * 12) + $height_in);
$weight = $_POST['lbs'];//conver this to kg

$userId = $myDatabaseFunctions->addUser($user, $_POST['Age'], $_POST['Gender'], $height, $weight );
?>

<form name='fr' action='final.php' method='POST'>
        <input type='hidden' name='user' value='<?=$user?>'>
        <input type="submit" />
</form>


<script type='text/javascript'>
document.fr.submit();
</script>

addUser()的代码:(当我在MySQL内部运行时,我构建的命令正在运行)

        public function addUser($user, $age, $gender, $height, $weight){
                //edit the information for a user after demographics page
                $stmt = $this->DB->prepare("UPDATE users SET age=:age, gender=:gender, height_cm=:height, weight_kg=:weight WHERE user_id=:user)");
                $stmt->bindParam('age', $age);
                $stmt->bindParam('gender', $gender);
                $stmt->bindParam('height', $height);
                $stmt->bindParam('weight', $weight);
                $stmt->bindParam('user', $user);
                $stmt->execute();

                //select last insert ID
                $stmt = $this->DB->prepare("SELECT LAST_INSERT_ID() from users");
                $stmt->execute();
                $userId = $stmt->fetch();
                return $userId[0];
        }

任何建议都将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:2)

如果您想创建用户,则必须使用UPDATE代替INSERT INTO users (user_id,age,gender,height_cm,weight_kg) VALUES (:user,:age,:gender,:height,:weight)

WITH oldstuff AS (
      SELECT name, foo, 'some value' AS bar
      FROM table1
     ),
     newstuff AS (
      INSERT INTO table2 (name)
          SELECT name
          FROM oldstuff
          RETURNING *
    )
INSERT INTO table3 (id2, name, foo, bar)
    SELECT ns.id, ns.name, os.foo, os.bar
    FROM newstuff ns join
         oldstuff os
         on ns.name = os.name;

答案 1 :(得分:-1)

尝试这个

<?php
session_start();

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', 'db_test');

error_reporting(E_ALL);
ini_set('display_errors', 1);

$conn=new mysqli(HOST,USER,PASS,DB);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
//=============================================
function addUser($user, $age, $gender, $height, $weight){
	//edit the information for a user after demographics page
	$stmt = $conn->prepare("UPDATE users SET user=?,age=?,gender=?,height_cm=?,weight_kg=? WHERE user_id=?)");
	$stmt->bindParam('sisiii', $user,$age,$gender,$height,$weight,$user_id);
	$stmt->execute();

	//select last insert ID
	$stmt = $conn->prepare("SELECT LAST_INSERT_ID() from users");
	$stmt->execute();
	$userId = $stmt->fetch_row();
	return $userId[0];
}

$_SESSION['array_original'];
if(isset($_POST["user"]) ){
    $user = $_POST["user"];
    $array = $_SESSION['array_original'];
}
$height_in = $_POST['in'];
$height_ft = $_POST['ft'];
$height = 2.54 * (($height_ft * 12) + $height_in);
$weight = $_POST['lbs'];//conver this to kg
$userId = addUser($user, $_POST['Age'], $_POST['Gender'], $height, $weight );
?>

<form name='fr' action='final.php' method='POST'>
        <input type='hidden' name='user' value='<?php=$user?>'>
        <input type="submit" />
</form>


<script type='text/javascript'>
document.fr.submit();
</script>