duanliang8464 2016-03-17 16:21
浏览 57

使用会话生成的信息更新mysql表

I'm currently doing a Web Programming module at university and have been having trouble with some of the homework set. We are meant to insert code that updates our current mysql table with new information (gender, age, email, comment). This information needs to be inserted into the row of each persons session generated ID (currID). How do we code for the updated information to be inserted into a session-specific row?

<?php
session_start();
include('muqHeader.html');
include('commonSrc.php');
include('../shareCode/mysqlLink.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST'):
// update the mf record 

if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
 }else{
     echo "Not a valid email address"; 
 }

if(filter_var($_POST['comment'], FILTER_SANITIZE_STRING)){
}else{
    echo "Text includes invalid characters";   
}

$gender = $_POST['gender'];
$age = $_POST['age'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$currID = $_SESSION['currID'];

if ($_POST['submit']){
$sql = "UPDATE muq
        SET (gender='$gender', age = '$age', email = '$email', comment =                 '$comment')
        WHERE (muqID = '$currID')"; 
}

    if (@mysqli_query($link, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . @mysqli_error($link);
}

else:

$useTime = implode(',', $_SESSION['useTime'] );
$usedM = implode( ',', $_SESSION['usedM'] );
$tmp = array();
for($i=0; $i < count($_SESSION['freqRate']); $i++) {
$tmp[$i] = implode( '', $_SESSION['freqRate'][$i] ); // empty string as     'glue'
}
$freqRate = implode( ',', $tmp );
$dateTime = $_SESSION['dateTime'];
$taskTime = (time() - $_SESSION['startTime']) / 60;   //in minutes
$sql = "INSERT INTO muq
    (dateTime, taskTime, useTime, usedM, freqRate)
    VALUES ('$dateTime', '$taskTime', '$useTime', '$usedM', 'freqRate')";
$link = connectDB();
@mysqli_query( $link, $sql );
$_SESSION['currID'] = @mysqli_insert_id($link);
@mysqli_close($link);

?>
  • 写回答

1条回答 默认 最新

  • dongxiezhi0590 2016-03-17 17:51
    关注

    Well, before answearing your question, here is some coding rules you need to respect: 1- You don't have to use more lines than what you need. This means you don't have to do an an empty "if" using 4 lines if you can do it in 2 lines. Example: Instead of:

    if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
    }else{
       echo "Not a valid email address"; 
    }
    

    You can do:

    if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
        echo "Not a valid email address";
    

    Second thing, to update a row in a database, you need an ID. This the key you are going to use to tell your db engine which row you are going to update because if not "he" will not know which row "he" should update (I'm considering the db engine as a person like me and you :D ) So, you need to inject that key (account ID or whatever) in your session so that you can use later when updating your database by telling you db engine that "he" needs to update that row identified by that key.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP