我创建了一个用户登录页面,用户使用他们的名称和密码登录数据库(DBname = signin,table = information)我想保持会话继续进行,无论用户继续访问哪个页面显示“HELLO和用户名”)。到目前为止,这完美无缺。
当进入AddRental页面时(只有地址信息)我必须创建一个新的数据库连接和会话到DB = project和table = address。但由于某种原因,它会在第2行附近出现错误SESSION_START();
我想我需要同时保持2次会议,但我不知道该怎么做。有没有人有什么建议?
非常感谢<?php
session_start();
$hostname="localhost"; // Host name
$username="root"; // phpmyadmin username
$password=""; // phpmyadmin password
$dbname="project"; // Database name
$tblname="address"; // Table name
// Connect to server and select databse.
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
// If form not yet submitted display form
// To check if the Submit button was clicked
if (isset($_POST['submit']))
{
// Get data entered into into form fields
$PostCode = $_POST['PostCode'];
$AddL1 = $_POST['AddL1'];
$AddL2 = $_POST['AddL2'];
$AddL3 = $_POST['AddL3'];
$County = $_POST['County'];
$Country = $_POST['Country'];
// Validation of the data entered into into form fields
//if ((preg_match('/^([A-Za-z]+ ?)*$/', $name))
//&& (preg_match('/^[A-Za-z0-9_]+([\.\-\+]{0,1}[A-Za-z0-9_])*@[A-Za-z0-9_]+([\.-]{0,1}[A-Za-z0-9_]+)*(\.[A-Za-z0-9]{2,4})+$/', $email))
//&& (preg_match('/^[A-Z][A-Z]/', $country))
//&& (preg_match('/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s)/', $password)))
{
// Insert data taken form fields into table
mysql_query("INSERT INTO `address` (`PostCode`, `AddL1`, `AddL2`, `AddL3`,`County`,`Country`)
VALUES('$PostCode', '$AddL1', '$AddL2', '$AddL3', 'County','Country'") or die(mysql_error());
}
}
// Close connection to database server
// mysql_close($mysql_connect);
?>
<!DOCTYPE html>
<html><head><title> Menu Form </title>
<link rel= "stylesheet" type="text/css" href="AddRental.css"/>
</head><body>
<div id="logo">
<img src="C:/Users/Daisy/Desktop/Website/Forms/FC.JPG" width="30%" height="5%"/>
<div id="addr"> Add Rental </div>
</div>
<div id="content">
<div id ="rform"> Insert Address
<br>
<FORM action = "AddRental.php" method="post">
<label for="PostCode">PostCode</label>
<input type="text" name="PostCode" id="PostCode" value="" maxlength="8" />
<br>
<label for="AddL1">AddL1</label>
<input type="text" name="AddL1" id="AddL1" value="" maxlength="40" />
<br>
<label for="AddL2">AddL2</label>
<input type="text" name="AddL2" id="AddL2" value="" maxlength="25" />
<br>
<label for="AddL3">AddL3</label>
<input type="text" name="AddL3" id="AddL3" value="" maxlength="25" />
<br>
<label for="County">County</label>
<input type="text" name="County" id="County" value="" maxlength="20" />
<br>
<label for="Country">Country</label>
<input type="text" name="Country" id="Country" value="" maxlength="20" />
<br>
<button type="submit" name="submit" >SUBMIT</button></div>
</FORM>
<div id ="lilmenu"> <?php echo "Hello ".$_SESSION['myusername']; ?>
<br>Options <br>
<FORM METHOD="LINK" ACTION="Welcome.php">
<INPUT TYPE="submit" VALUE="Back"></FORM><br>
<FORM name="LogOut" method="post" action="logout.php">
<INPUT name="LogOut" TYPE="submit" VALUE="Log-Out"></FORM>
</div>
</div>
<div id="footer"> © Copyright 2014 </div>
</body></html>
答案 0 :(得分:0)
从你的评论中,你得到"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2"
,这是由你的SQL查询引起的,而不是会话。
我不支持mysql_*
功能,但在您使用时,请确认您在使用mysql_real_escape_string()
之前清理输入。否则输入中的'
或"
等特殊字符可能会导致SQL查询出错。