简体   繁体   中英

How to get a MySQL field's content with PHP?

I've been working on my website's database and I had this problem recently: I want the user to read a field of a row, when another field of the same row is submitted. Ie:

user_id=1 user_name=Fran user_pass=Potato referedby_id=0 referedby_name=empty

When going inside www.website.com/form.php?referedby_id=1

I want the user to see "So, Fran refered you?"

I've been learning php and I decided to try this:

$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.

The problem is that when I try this and check the database, the referedby_name field is empty. Is it a syntax error? Or is this because it didn't convert to text properly?

In case $referedbyname is not text, how can I convert it properly? Is this because I'm using the fetch_field function wrong?

Additional info : $referedbyid is being called properly (I think) in a POST form with this html

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

I don't know why the downvotes, I tried to be as clear and specific as I could... Anyways, if someone else has any trouble finding the value of a specific field just use mysqli_fetch_assoc(mysqli_query($conect, $select))['name of the sql field']

mysqli_fetch_field($result) will only show you information about the field, like the type of input, name of the table, name of the column, etc.

Hope that's useful for someone with the same issue. Bye.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM