2013-05-11 144 views
0

这里是我的代码不能正常工作输入表单数据不保存在mysql数据库中?

<?php 
include('admin/class.php'); 

这里是我的数据库连接:

$hostname="localhost"; 
$username="root"; 
$password=""; 
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL"); 
echo "Connected to MySQL<br>"; 
$se = mysql_select_db("timesheet1234",$dbhandle) 
    or die("Could not select timesheet1234"); 
echo "connected to db"; 

这里点击按钮后,现在干的功能:

if(isset($_POST['save'])) 
{ 
[email protected]$_POST['selpro']; 
[email protected]$_POST['mon']; 
[email protected]$_POST['tue']; 
[email protected]$_POST['wed']; 
[email protected]$_POST['thu']; 
[email protected]$_POST['fri']; 
[email protected]$_POST['sat']; 
[email protected]$_POST['sun']; 

我在这里写的查询功能

if(isset($_SESSION['user'])) 
{ 
echo "session user"; 
$sql="UPDATE empdaytimesheet SET 
`project code`='$sel',`mon`='$mon',`tue`='$tue',`wed`='$wed',`thu`='$thu',` 
fri`='$fri',`sat`='$sat',`sun`='$sun' where `username`='".$_SESSION['user']."'"; 

$res=mysql_query($sql,$dbhandle); 

这里是它无法正常工作的问题:

if($res){ 
     echo "<script type='text/javascript'>"; 
     echo "alert('TimeSheet Saved..!')"; 
     echo "</script>"; 
     echo "<script type='text/javascript'>"; 
     echo "window.location='my_timesheet.php'"; 
     echo "</script>"; 
     } 
     else 
     { 
     echo "<script type='text/javascript'>"; 
     echo "alert('Some Error Occured ! Retry..!')"; 
     echo "</script>"; 
     echo "<script type='text/javascript'>"; 
     echo "window.location='my_timesheet.php'"; 
     echo "</script>"; 
     } 
    } 
    } 
?> 
+0

可以进行测试,但不要在实时网站上使用mysql_ *函数,因为它们已被弃用且不太安全。另外,当索引未定义在$ _POST中时,抑制错误不是执行它的方式,错误仍然会发生,您只是没有看到它。 – adeneo 2013-05-11 12:41:48

+0

你有输出吗? – 2013-05-11 12:42:56

+0

是的,我得到了成功的警报,保存时间表 – 2013-05-11 12:59:57

回答

0

删除“@”之前,$ _ POST,你应该罚款。

但是,您当前的脚本看起来很容易受到SQLi的影响。

而且您还使用了不推荐的扩展。您应该考虑使用MySQLi或PDO。

Check this answer关于如何使您的代码更安全的更多信息。

+0

不,它在使用mysqli的时候没有连接到数据库 – 2013-05-11 12:52:28

+0

那么你可能做错了。发布您用于MySQLi的代码,我将尽我所能帮助您。 – 2013-05-11 13:18:55

+0

<?php include('admin/class.php'); $ hostname =“localhost”; $ username =“root”; $ password =“”; $ dbhandle = mysqli_connect($ hostname,$ username,$ password) 或死(“无法连接到MySQL”); echo“连接到MySQL
”;如果(isset($ _ POST ['save'])) { $ sel = @ $ _ POST ['selpro']; $ mon = @ $ _ POST ['mon']; $ tue = @ $ _ POST ['tue']; $ wed = @ $ _ POST ['wed']; $ thu = @ $ _ POST ['thu']; $ fri = @ $ _ POST ['fri']; $ sat = @ $ _ POST ['sat']; $ sun = @ $ _ POST ['sun']; – 2013-05-11 13:20:48