我对MySQL很新。我试图创建一个从html表单中读取数据并将其存储到数据库中的php脚本。我还上传了一个路径保存在数据库中的图像,图像本身也存储在c:wamp / www / uploads中。现在,当我在提交表单后在我的wamp服务器上运行脚本时,我得到一个空白页面。当我检查我的上传文件夹时,它仍然是空的。因此图像不会放入文件夹中。任何人都可以调试吗?
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'userdatadelta');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$table= "CREATE TABLE `users`
( `rollno` int(15) NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`imageid` varchar(50) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`email`))";
mysqli_query($db,$table);
if(isset($_POST["submit"]))
{
$name = $_POST["name"];
$roll_number = $_POST["rollno"];
$department = $_POST["department"];
$year = $_POST["year"];
$email = $_POST["email"];
$password = $_POST["password"];
$filename=$_FILES['userpic']['name'];
$filetype=$_FILES['userpic']['type'];
$name = mysqli_real_escape_string($db, $name);
$roll_number = mysqli_real_escape_string($db, $roll_number);
$department = mysqli_real_escape_string($db, $department);
$year = mysqli_real_escape_string($db, $year);
$email = mysqli_real_escape_string($db, $email);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$newfilename= $roll_number;
if($filetype=='image/jpeg' or $filetype=='image/png' or $filetype=='image/gif')
{
move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$newfilename);
$filepath="upload/".$newfilename;
}
$sql = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
echo "An account has been created with this email ID already. We regret the inconvenience";
}
else
{
$query = mysqli_query($db, "INSERT INTO users (name, rollno, department, year, email, password, imagepath)VALUES ( '$name','$roll_number', $department, $year,'$email', '$password', '$filepath')");
if($query)
{
echo "Thank You! You have completed registration and are now registered.";
}
}
}
?>
编辑代码,大部分工作但插入数据:(。2条评论&#34;已经使用此电子邮件ID创建了一个帐户。我们对此给您带来的不便感到遗憾&#34;和#34;谢谢!您已完成注册,现已注册。&#34;似乎无法正常工作。
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$DB_SERVER="localhost";
$DB_USERNAME="root";
$DB_PASSWORD="";
$DB_DATABASE="userdatadelta";
$db = mysqli_connect( "$DB_SERVER" ,"$DB_USERNAME","$DB_PASSWORD","$DB_DATABASE")or die("Cannot connect");
echo "Got connected?";
if(isset($_POST["submit"]))
{
echo "Got inside isset!";
$name = $_POST["name"];
$roll_number = $_POST["rollno"];
$department = $_POST["department"];
$year = $_POST["year"];
$email = $_POST["email"];
$password = $_POST["password"];
$filename=$_FILES['userpic']['name'];
$filetype=$_FILES['userpic']['type'];
$name = mysqli_real_escape_string($db, $name);
$roll_number = mysqli_real_escape_string($db, $roll_number);
$department = mysqli_real_escape_string($db, $department);
$year = mysqli_real_escape_string($db, $year);
$email = mysqli_real_escape_string($db, $email);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$newfilename= $roll_number;
if($filetype=='image/jpeg' or $filetype=='image/png' or $filetype=='image/gif')
{
echo "Got inside file type checking!";
move_uploaded_file($_FILES['userpic']['tmp_name'],'upload/'.$newfilename);
$filepath="upload/".$newfilename;
}
$sql = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
echo "An account has been created with this email ID already. We regret the inconvenience";
}
else
{
$query = mysqli_query($db, "INSERT INTO users (name, rollno, department, year, email, password, imagepath)VALUES ( '$name','$roll_number', $department, $year,'$email', '$password', '$filepath')");
echo "Got inside else!";
if($query)
{
echo "Thank You! You have completed registration and are now registered.";
}
}
}
echo "Comment!";
?>
答案 0 :(得分:0)
使用如此长的代码段很难确定可能的错误,因为空白页可能是由多种原因引起的。
可能与您的代码相关的一些典型问题:
PHP语法错误,例如额外或缺少大括号({
或}
)。你发布的那篇文章似乎还可以,但是你不知道你使用的是哪个PHP版本。
拒绝写入权限上传路径。因为权限错误或路径不正确。您没有指定您的操作系统,但它被假定为Windows。有些系统使用不区分大小写的路径,其他区分大小写,请检查当前系统的版本和配置。
对于空白页面问题,即使没有引发错误,也会执行一系列执行,其中两个echo
调用中没有一个被触发。
简而言之,PHP不会向用户输出任何错误,因为在生产环境中,它可能会带来安全风险。
在您的开发环境中,您可以启用错误输出以快速解决出错的问题。
在第一次打开 <?php
子句后立即添加以下行:
error_reporting(E_ALL);
ini_set("display_errors", 1);
现在,PHP抱怨的所有错误都应输出到浏览器。
请注意,这在生产环境中不被视为特别好的做法。应该在日志文件中查找错误。用它来帮助自己在开发时调试和解决当前问题。如果您正在处理大型项目,请考虑获取PHP框架的帮助以增强编码体验。