dongpi0658 2016-02-19 10:41
浏览 53
已采纳

如何使用php回显从文本框表单发布的变量?

update : You can understand my problem if you see my test link. http://smstostudents.com/rmk/index3.php (select any name and submit)

Here is my HTML form code

<input type="checkbox" name="student[]" value="1"><label>student name 1/label>
<input type="checkbox" name="student[]" value="2"><label>student name 2</label>
<input type="checkbox" name="student[]" value="3"><label>student name 3</label>

<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear $studentname you have not paid the fees, please pay it as soon as possible.
</textarea><br />

The Php code is as follows.

<?php
$message = $_POST['message'];
$servername = "localhost";
$dbusername = "xxxxxx";
$dbpassword = "xxxxxxx";
$dbname = "xxxxxxxx";
foreach($_POST['student'] as $value)
{
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
    $studentname = $getID['firstname'];
    $email = $getID['email'];
    echo $message;
}
?>

What I am trying to achieve is, in the form text box "$studentname" will be replaced by student's name from the database. But echo $message doesn't display the students name, instead it just displays,

"Dear $studentname you have not paid the fees, please pay it as soon as possible."

how can I replace the "$studentname" by the Student's name. Sorry if it is easy, but I am stuck. Sorry, my English is bad, and I am a beginner in coding.

Update :

Actually i have no problem in the form. The form correctly posts the values to php. But, the textbox value contains "$studentname", which must be replaced with the student's name which is taken from the Database. But, the php code is not doing it, it is juts diplaying the textbox content as it is.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dqyitt2954 2016-02-19 11:14
    关注

    First, you are connecting to the database inside a foreach loop... this is bad, and will make your script slower. Try moving the connection outside of the loop.

    Second, you are querying the database multiple times for data you want to gather from a single table. There is an easier way to do this.

    Here is an example:

    <?php
    
    
    $message = $_POST['message'];
    $servername = "localhost";
    $dbusername = "smstobdc_smstest";
    $dbpassword = "removable";
    $dbname = "smstobdc_smstest";
    
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    //First we need to sanitize the values. or you will be open to a mysql injection...
    $students = array_map('mysql_real_escape_string',$_POST['students']); // this sanitizes all values inside $_POST['students'];
    //now let's prepare them for the query
    $students = "'".implode("','",$students)."'"; //this formats it like so: '1','2','3','4'....
    //now let's perform our query.. if you want to fetch all the results, then loop like this...
    //Make sure you perform the query and save the result resource so you can catch an error like so...
    $result = mysqli_query($conn, 'SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('.$students.')')
    //The actual query looks like this:
    //    SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('1','2','3','4','5','6'[,etc..])
    
    //now we can iterate through the results for each student. This while loop will continue until all the data specified by your student id's is returned
    while($getID = mysqli_fetch_assoc($result)) {
    
        $studentname = $getID['firstname'];
        $email = $getID['email'];   
    
        //NOTE: $studentname in $message is literally "$studentname". PHP will not treat this as a variable... what you can do is replace the text...
        echo str_replace('$studentname',$studentname,$message);
    }
    ?>
    

    If you just want the quick and dirty way to do it with your code then here is that as well:

    <?php
    
    
    $message = $_POST['message'];
    $servername = "localhost";
    $dbusername = "smstobdc_smstest";
    $dbpassword = "removable";
    $dbname = "smstobdc_smstest";
    
    
    
    foreach($_POST['student'] as $value) {
    
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
    $studentname = $getID['firstname'];
    $email = $getID['email'];   
    
        echo str_replace('$studentname',$studentname,$message);
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵