更新:如果您看到我的测试链接,您可以理解我的问题。 http://smstostudents.com/rmk/index3.php (选择任何名称并提交)
这是我的HTML表单代码
<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 />
Php代码如下。
<?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;
}
?>
我想要实现的是,在表格文本框&#34; $ studentname&#34;将由数据库中的学生姓名替换。但echo $ message不显示学生姓名,而只是显示,
&#34;亲爱的学生姓名,您尚未支付费用,请尽快支付。&#34;
如何更换&#34; $ studentname&#34;以学生的名字命名。对不起,如果这很容易,但我被卡住了。 对不起,我的英语不好,我是编码的初学者。
更新:
其实我在表格上没有问题。表单正确地将值发布到php。 但是,文本框值包含&#34; $ studentname&#34;,必须替换为从数据库中获取的学生姓名。 但是,php代码并没有这样做,它正是将文本框内容放在原文中。
提前致谢。
答案 0 :(得分:1)
试试这个(你必须将php添加到echo学生名称标签):
<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear <?php echo $studentname; ?> you have not paid the fees, please pay it as soon as possible.
</textarea><br />
答案 1 :(得分:0)
试试这个
<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear
<?php
echo $studentname ;
?>
you have not paid the fees, please pay it as soon as possible.
</textarea><br />
答案 2 :(得分:0)
首先,你在foreach循环中连接到数据库......这很糟糕,会使你的脚本变慢。尝试将连接移到循环外部。
其次,您要多次查询数据库以查找要从单个表中收集的数据。有一种更简单的方法可以做到这一点。
以下是一个例子:
<?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);
}
?>
如果您只是想用快速而肮脏的方式来处理代码,那么这里也是如此:
<?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);
}
?>