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.

图片转代码服务由CSDN问答提供 功能建议

更新:如果看到我的测试链接,您可以理解我的问题。 http://smstostudents.com/rmk/index3.php (选择任何名称) 这是我的HTML表单代码

 &lt; input type =“checkbox”name =“student []”value  =“1”&gt;&lt;标签&gt;学生姓名1 /标签&gt; 
&lt; input type =“checkbox”name =“student []”value =“2”&gt;&lt; label&gt;学生姓名2&lt; / label&gt;  
&lt; input type =“checkbox”name =“student []”value =“3”&gt;&lt; label&gt;学生姓名3&lt; / label&gt; 
 
&lt; textarea id =“message”class =“input”  name =“message”rows =“7”cols =“50”&gt; 
 
 $ $ studentname您尚未支付费用,请尽快付款。
&lt; / textarea&gt;&lt; br /&gt; 
    
 
 

Php代码如下。

 &lt;?php 
 $ message = $ _POST ['message']; 
 $ servername =“localhost”; 
 $ dbusername =“xxxxxx”; \  n $ dbpassword =“xxxxxxx”; 
 $ dbname =“xxxxxxxx”; 
foreach($ _ POST ['student'] $ value)
 {
 //创建连接
 $ conn = new mysqli($ servername  ,$ dbusername,$ dbpassword,$ dbname); 
 //检查连接
 if($ conn-&gt; connect_error){
 die(“Connection failed:”。$ conn-&gt; 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; 
} 
?&gt; 
   
 
 

我想要实现的是,在表格文本框“$ studentname“将由数据库中的学生姓名替换。 但echo $ message不显示学生姓名,而只是显示,

“亲爱的学生姓名,你还没有支付费用,请尽快支付。”

如何用学生的名字替换“$ studentname”。 对不起,如果这很容易,但我被卡住了。 很抱歉,我的英语很糟糕,我是编码的初学者。

更新: < p>其实我在表格上没有问题。 表单正确地将值发布到php。 但是,文本框值包含“$ studentname”,必须替换为从数据库中获取的学生名称。 但是,php代码没有这样做,它是将文本框内容原样放在一边。 / p>

提前致谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题