doucheng9634 2014-04-01 22:13
浏览 58
已采纳

mysqli_query不接受我的mysqli_connect并正确使用mysqli_query的参数

i have set my configdb.php on a different page and include it on my other php pages..

here is my configdb.php

<?php
$hostname ="localhost";
$username ="root";
$password ="";
$db ="practicedb";
$connect = mysqli_connect($hostname,$username,$password) or die("cannot connect to server");
mysqli_select_db($connect,$db) or die("database not found!");
?>

these are the errors that i get:

Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 15

Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 28

this is my add.php where i INSERT items into database from the $_POST method from a previous php page..

<?php
include "configdb.php";



$studid=$_POST['studid'];
$lastname=mysql_real_escape_string($_POST['lastname']);
$firstname= mysql_real_escape_string($_POST['firstname']);
$middlename= mysql_real_escape_string($_POST['middlename']);
$email=$_POST['email'];


$check = "SELECT * from studinfo where stud_id = '".$studid."'";
$qry = mysqli_query($configdb,$check);
$num_rows = mysqli_num_rows($qry); 


if($num_rows > 0){
// Here we are checking if username is already exist or not.

    echo "The person you have entered is already existing. Please try again.";
    echo '<a href="index.php">Try Again</a>';
    exit;
}

$query = "INSERT INTO studinfo (stud_id,lastname,firstname,middlename,email) VALUES ('".$studid."','".$lastname."','".$firstname."','".$middlename."','".$email."');";
//echo $query;
mysqli_query($configdb, $query);
echo "Thank You for Registration.";
echo '<a href="index.php">Click Here</a> to login you account.';
exit;
?>

i don't know and i am not sure what to put on the first parameter of mysqli_query.. i tried putting this code $con=mysqli_connect("localhost","root","","practicedb"); it worked but its not practical putting that on every php page where i should connect to the database...

  • 写回答

1条回答 默认 最新

  • dongwalun2507 2014-04-01 22:15
    关注

    Yet another question on a silly typo...

    $connect = mysqli_connect( ...
    vs. $qry = mysqli_query($configdb,$check);

    so the error message clearly says: Undefined variable: configdb

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

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作