Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql插入和更新_Php_Mysql_Insert - Fatal编程技术网

Php Mysql插入和更新

Php Mysql插入和更新,php,mysql,insert,Php,Mysql,Insert,我需要一些关于理解mysql插入和更新方法的帮助。我正在运行Mamp,我知道php和mysql正在工作。我的数据库的设置方式是,它们在info表中有一个键。数据在进入数据库时被加密,在显示到passbook.html时被解密。当前可以更新数据,但如果更改用户名或密码,则会在数据库中放置一个新条目,而不是更新它。如何解决此问题以更新数据。如果更改了密码或用户名,则应进行更新。如果网站被更改,它应该创建一个新条目并删除旧条目 passbook.html <!DOCTYPE html> &

我需要一些关于理解mysql插入和更新方法的帮助。我正在运行Mamp,我知道php和mysql正在工作。我的数据库的设置方式是,它们在info表中有一个键。数据在进入数据库时被加密,在显示到passbook.html时被解密。当前可以更新数据,但如果更改用户名或密码,则会在数据库中放置一个新条目,而不是更新它。如何解决此问题以更新数据。如果更改了密码或用户名,则应进行更新。如果网站被更改,它应该创建一个新条目并删除旧条目

passbook.html

<!DOCTYPE html>
<html>
<head>
    <title>Passbook</title>
</head>
<body >

 <header>

<h1> My Passbook</h1></header>
</header>
<body>        
<?php
    $count = 1;
?>

 <style>
#pass a{
visibility:hidden;
}
#pass:hover a{
visibility:visible;
}
td{
    text-align:center;
}
</style>

    <script type="text/javascript">
function checkBox(checkbox){
if(checkbox.checked)
    checkbox.value = 'Yes';
else
    checkbox.value = 'No';
}

function addRow(){
            var table = document.getElementById('table');
            var row = table.insertRow(-1);


            var count = document.getElementById('table').rows.length - 1;

            row.innerHTML="<td name='row' value='"+count+"'>"+count+"</td>"
                        + "<td><input name='" + count + "check' type='checkbox' value=''></input></td>"
                        + "<td><input name='" + count + "web' type='text'></input></td>"
                        + "<td><input name='" + count + "user' type='text'></input></td>"
                        + "<td><input name='" + count + "pass' type='text'></input></td>";


                        document.getElementById('custom').value = count;

            }

</script>

<form method="post" action="passbook.php">
 <table id="table" border=5;>
<th>Row #</th>
<th>Selected</th>
<th>Websites</th>
<th>Usernames</th>
<th>Passwords</th>

<?php
session_start();
$connection = mysql_connect('localhost', $_SESSION['username'], $_SESSION['password']) or die ("Could not connect");
$key = mysql_query('SELECT `key` FROM `'.$_SESSION['username'].'`.`info`');
$string = 'SELECT * FROM `'.$_SESSION['username'].'`.`passbook` WHERE 1';
$result = mysql_query($string);
    while($row = mysql_fetch_assoc($result))
    {
        $decUser = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($row['username']), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
        $decPass = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($row['password']), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
        if($row['selected'] == 1 )
            echo "<tr><td style='text-align:center;' value='".$count."'>".$count."</td>
                  <td><input type='checkbox'  name='".$count."check' value='1' checked></td>
                  <td><input type='text' name='".$count."web' value='".$row['website']."'><br></td>
                  <td id='user'><a><input type='text' name='".$count."user' value='".$decUser."'></a></td>
                  <td id='pass'><a><input type='text' name='".$count."pass' value='".$decPass."'></a></td>
                  </tr>";
        else if($row['selected'] == 0)
            echo "<tr><td style='text-align:center;' value='".$count."'>".$count."</td>
                  <td><input type='checkbox'  name='".$count."check' value='0'></td>
                  <td><input type='text' name='".$count."web' value='".$row['website']."'><br></td>
                  <td id='user'><a><input type='text' name='".$count."user' value='".$decUser."'></a></td>
                  <td id='pass'><a><input type='text' name='".$count."pass' value='".$decPass."'></a></td>
                  </tr>";
    $count++;
    }
    ?>
    </table>


    <br/>
 <input type="button" value="Add row" id="AddRow" onclick="addRow()"></input>
 <input type="submit" value="Save Table"></input>
 </form>
    <br/><br/>
 <label id="yes"></label>

 <script type="text/javascript">
 function GotoPage()
 {
 var loc = document.getElementById('scroller').value;
 if(loc!="0")
 window.location = loc;
 }
 </script>


 <select id="scroller" style="height:42px; width:145px;" onchange="GotoPage()" >
 <optgroup label="Home">
<option value="homepage.html">Home</option>
</optgroup>
<optgroup label="View Credentials" >
<option value="passbook.html" selected>Passbook</option>
</optgroup>
<optgroup label="Other" >
 <option value="settings.html" >Settings</option>
</optgroup>
</select>

</body></html>

存折
我的存折
#通过{
可见性:隐藏;
}
#通过:悬停a{
能见度:可见;
}
运输署{
文本对齐:居中;
}
功能复选框(复选框){
如果(复选框。选中)
checkbox.value='Yes';
其他的
checkbox.value='No';
}
函数addRow(){
var table=document.getElementById('table');
var行=table.insertRow(-1);
var count=document.getElementById('table').rows.length-1;
行。innerHTML=“+count+”“
+ ""
+ ""
+ ""
+ "";
document.getElementById('custom')。value=count;
}
划船#
挑选出来的
网站
保护用户名
密码

您的PHP有重大错误

  • mysql_uu函数正在贬值
  • 您将用户提交的值直接发送到数据库,使您容易受到SQL注入的攻击
  • 你的代码不符合逻辑
在下面的代码中,您尝试将数组-$info-与字符串-$website进行比较

while($info = mysql_fetch_assoc($select)){
    if($info == $website){
        $query = "UPDATE `passbook` SET  `selected` = '1',`username` = '".$encUser."',`password`='".$encPass."' WHERE `website`='".$website."'";
        $run = mysql_query($query);
    }
}
我想你的意思是:

while($info = mysql_fetch_assoc($select)){
    // $info is an array that holds the row's data.  You access an individual "cell" by referencing the column name as the key
    if($info['website'] == $website){
        $query = "UPDATE `passbook` SET  `selected` = '1',`username` = '".$encUser."',`password`='".$encPass."' WHERE `website`='".$website."'";
        $run = mysql_query($query);
    }
}
然后在这段代码中检查$q2是否已设置,如果未设置,则尝试使用它执行查询?没有道理

if(!isset($q2)){
    $run = mysql_query($q2);
}

这比什么都重要,但……你的“加密”方案毫无意义。您将密钥与加密数据一起存储。如果有人能够访问您的数据库(如果您不解决SQL注入问题,他们会这样做),他们将拥有解密数据所需的一切。

谢谢。您会推荐什么样的加密方案?
if(!isset($q2)){
    $run = mysql_query($q2);
}