PHP执行SQL不刷新页面?希望各位帮忙 我刚接触PHP
<?php//Ifalltherequiredfieldsarefilledif(!empty($_GET['fullname'])&&!empty($_GET['ema...
<?php
//If all the required fields are filled
if (!empty($_GET['fullname'])&&!empty($_GET['email'])&&!empty($_GET['phone']))
{
$servername = "root";
$username = "root";
$password = "root";
// Create connection
$con = mysql_connect($servername,$username,$password);
// Check connection
if (!$con){
die('Could not connect: ' . mysql_error());}
mysql_select_db("teamproject", $con);
mysql_select_db("my_db", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());}
echo "one record added";
}
else //If the form is not submited or not all the required fields are filled
mysql_close($con);
?>
<form>
<fieldset>
<legend>Add New Contact</legend>
<input type="text" name="fullname" placeholder="First name and last name" required /> <br />
<input type="email" name="email" placeholder="etc@company.com" required /> <br />
<input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br />
<input type="submit" name="submit" class="button" value="Add Contact"/>
<input type="button" name="cancel" class="button" value="Reset" />
</fieldset>
</form>
mysql_select_db("teamproject", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
$chk="SELECT * FROM contact where fullname='$_GET[fullname]'&&phonenumber='$_GET[phone]'&&email='$_GET[email]';";
$test=mysql_fetch_array(mysql_query($chk,$con));
if($test==false){
mysql_query($sql,$con);
echo "<script>alert('one record added')</script>";}
else{
echo "<script>alert('already record')</script>";
}
修改了一部分 展开
//If all the required fields are filled
if (!empty($_GET['fullname'])&&!empty($_GET['email'])&&!empty($_GET['phone']))
{
$servername = "root";
$username = "root";
$password = "root";
// Create connection
$con = mysql_connect($servername,$username,$password);
// Check connection
if (!$con){
die('Could not connect: ' . mysql_error());}
mysql_select_db("teamproject", $con);
mysql_select_db("my_db", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());}
echo "one record added";
}
else //If the form is not submited or not all the required fields are filled
mysql_close($con);
?>
<form>
<fieldset>
<legend>Add New Contact</legend>
<input type="text" name="fullname" placeholder="First name and last name" required /> <br />
<input type="email" name="email" placeholder="etc@company.com" required /> <br />
<input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br />
<input type="submit" name="submit" class="button" value="Add Contact"/>
<input type="button" name="cancel" class="button" value="Reset" />
</fieldset>
</form>
mysql_select_db("teamproject", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
$chk="SELECT * FROM contact where fullname='$_GET[fullname]'&&phonenumber='$_GET[phone]'&&email='$_GET[email]';";
$test=mysql_fetch_array(mysql_query($chk,$con));
if($test==false){
mysql_query($sql,$con);
echo "<script>alert('one record added')</script>";}
else{
echo "<script>alert('already record')</script>";
}
修改了一部分 展开
3个回答
展开全部
不知道是你写错了还是编码的时候没注意,多写了 else ,还有选择数据库的时候写了两遍。不知道你到底用哪个!
<?php
if (!empty($_GET['fullname'])&&!empty($_GET['email'])&&!empty($_GET['phone']))
{
$servername = "localhost";
$username = "root";
$password = "root";
$con = mysql_connect($servername,$username,$password);
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("test", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
echo "one record added";
mysql_close($con);
}
?>
<?php
if (!empty($_GET['fullname'])&&!empty($_GET['email'])&&!empty($_GET['phone']))
{
$servername = "localhost";
$username = "root";
$password = "root";
$con = mysql_connect($servername,$username,$password);
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("test", $con);
$sql="INSERT INTO contact (fullname, phonenumber, email) VALUES ('$_GET[fullname]','$_GET[phone]','$_GET[email]')";
if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
echo "one record added";
mysql_close($con);
}
?>
追问
呃 之前没什么注意 写多了 谢谢你告诉我 但我还是不清楚要如何不刷新页面执行SQL
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要ajax 来实现 , 会js吗? 如果学php 不会js 很悲催的 !
追问
js 我会 但也处于初学者行列 是不是用XMLHttpRequest?
追答
ajax的基本操作
iphone
xiaomi
smartisan
var iphone = document.getElementById('iphone');
var xiaomi = document.getElementById('xiaomi');
var smartisan = document.getElementById('smartisan');
//获取所有的手机节点
var phones = document.getElementsByClassName('phone');
//遍历绑定事件
for(var i=0;i
你新建个2.php 里面echo 一些内容 你试试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jquery+ajax 实现会快速上手
追问
可以具体的说一说吗
追答
先学习javascript 然后在学习jquery,也可以去w3cschool入门学习ajax
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询