Home > database >  Try to get data from table and insert into my database
Try to get data from table and insert into my database

Time:11-02

Can anyone help with this:

I am trying to collect email if the user likes a movie and update my database. I need two variables now: email and id. For email, I used a form to collect the user's email, but how do I collect the id without letting the user manually input that? For example, if the user submitted his/her email at the row of 101, how does the computer know that the id should be 101 and vice versa?

Here is what I got now for the table:

echo "<tr>
        <td style='text-align:center'>".$value['id']."</td>
        <td style='text-align:center'>".$value['name']."</td>
        <td style='text-align:center'>".$value['rating']."</td>
        <td style='text-align:center'>".$value['production']."</td>
        <td style='text-align:center'>".$value['budget']."</td>
        <td>
            <form action='like.php' method='post'>
                E-mail: <input type='text' name='email3'><br>
                <input type='submit'>
            </form>
        </td>
        </tr>";

The like.php is the place that will do the SQL. The table looks like this: enter image description here

CodePudding user response:

You can just let the user see a button and hide the other info, I also assume you only need an id as that should uniquely identify the user

echo "<tr>
        <td style='text-align:center'>".$value['id']."</td>
        <td style='text-align:center'>".$value['name']."</td>
        <td style='text-align:center'>".$value['rating']."</td>
        <td style='text-align:center'>".$value['production']."</td>
        <td style='text-align:center'>".$value['budget']."</td>
        <td>
            <form action='like.php' method='post'>
                <input type='text' name='id' value=" . $value['id'] ."><br>
                <input type='submit' name="like" value="LIKE">
            </form>
        </td>
        </tr>";

Now the like.php just reads the `id from the $_POST and uses it to maintain the like system.

You can also stop showing the user their id when you do this by removing this line

<td style='text-align:center'>".$value['id']."</td>

Alternatively, you should know which user is logged in, and that detail may be kept in the SESSION

CodePudding user response:

You can use a hidden input type. According to MDN docs

<input> elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted. For example, the ID of the content that is currently being ordered or edited, or a unique security token. Hidden inputs are completely invisible in the rendered page, and there is no way to make it visible in the page's content.

Modify your code as below

echo "<tr>
        <td style='text-align:center'>".$value['id']."</td>
        <td style='text-align:center'>".$value['name']."</td>
        <td style='text-align:center'>".$value['rating']."</td>
        <td style='text-align:center'>".$value['production']."</td>
        <td style='text-align:center'>".$value['budget']."</td>
        <td>
            <form action='like.php' method='post'>
                E-mail: <input type='text' name='email3'><br>
                <input type="hidden" value=". $value['id'] ." name"hidden_id">
                <input type='submit'>
            </form>
        </td>
        </tr>";

Then you can access the id in your like.php as follows

$id = $_POST['hidden_id']
  • Related