我一直在我网站的数据库上工作,最近我遇到了这个问题: 我希望用户在提交同一行的另一个字段时读取一行的字段。 即:
user_id = 1 user_name = Fran user_pass = Potato referedby_id = 0 referedby_name = empty
进入www.website.com/form.php?referedby_id=1
时我希望用户看到"所以,Fran提到了你?"
我一直在学习php,我决定试试这个:
$referedbyid = mysqli_real_escape_string($con,$_POST["referedby_id"]); //In this case it's 1 because of the url
$sel_referedbyname = "select user_name from users where user_id='$referedbyid'"; //Then this should be the select of the user_name "Fran"
$run_referedbyname = mysqli_query($con, $sel_referedbyname); //Then a query for that select
$check_referedbyname = mysqli_fetch_field($run_referedbyname); //And this one the content of the query's result
$refername = $check_refername->user_name; //As the query result is an object I want to convert it to text
if(isset($_POST["register"]) && $check_user == 0 && $check_email == 0 && $pass == $pass2){ //If everything is right and the user/email does not already exist
mysqli_query($con,"INSERT INTO users (user_name, user_email, user_pass, refer_id, refer_name) VALUES ('$user', '$email', '$pass', '$referid', '$refername')"); //It's submitted to the database with the other values.
mysqli_close($con); //And we finish the connection with the db.
问题是,当我尝试这个并检查数据库时,referedby_name字段为空。这是语法错误吗?或者这是因为它没有正确转换为文本?
如果$ referedbyname不是文本,我该如何正确转换它?这是因为我使用fetch_field函数错了吗?
其他信息:使用此html在POST表单中正确调用$ referedbyid(我认为)
<form action="register.php" method="post" onsubmit="validate();">
<table width="500" align="center" bgcolor="skyblue">
<tr align="center">
<td colspan="3"><h2>Registrarse</h2></td>
</tr>
<tr>
<td align="right"><b>Nombre de usuario:</b></td>
<td><input type="text" name="user" required="required"/></td>
</tr>
<tr>
<td align="right"><b>Email:</b></td>
<td><input type="email" name="email" required="required"/></td>
</tr>
<tr>
<tr>
<td align="right"><b>Contraseña:</b></td>
<td><input type="password" id="pass" name="pass" required="required"/></td>
</tr>
<tr>
<td align="right"><b>Verificar contraseña:</b></td>
<td><input type="password" id="pass2" name="pass2" required="required"/></td>
</tr>
<tr align="center">
<td colspan="3">
<input type="hidden" name="referedby_id" value="<?php echo $_GET["referedby_id"]; ?>"/>
<input type="submit" name="register" value="Registrarse"/></td>
</tr>
</table>
</form>
答案 0 :(得分:1)
我不知道为什么会掉下来,我试着尽可能清楚和具体......
无论如何,如果其他人在找到特定字段的值时遇到任何问题,请使用screenBounds.width
mysqli_fetch_assoc(mysqli_query($conect, $select))['name of the sql field']
只会显示有关该字段的信息,例如输入类型,表名称,列名称等。
希望对有同样问题的人有用。再见。