user12654809
user12654809

Reputation: 19

Add new elements to mysql with html

I am trying to add new elements to my database on mysql through my webpage.

This is what I've got so far

<form action="MyCurrentPage.php" method="post" >
   <label for="playername"> Player </label>
   <input type="text" name="addplayer" id="playername"/>
   <input type= "submit" value="submit" />
</form>

and this

<?php
     if (isset($_POST['submit'])) {
        $addplayerv=$_POST['addplayer'];
        $mysqli->select_db("player", $player);

        $sql="INSERT INTO player (nameofplayer) VALUES ('".$addplayerv."')";
        $mysqli->query($sql, $mysqli);
        $mysqli->close($mysqli);
     }

?>

The problem with this is that its not updating anything and I am not getting any errors.

Upvotes: 0

Views: 59

Answers (4)

M.Clark
M.Clark

Reputation: 29

Please add attribute name on Submit form. Because if you did not mentioned the name attribute, the data which you want to post for add is not entered into condition which you have written under the PHP tags.

Please change the HTML with this:

<form action="MyCurrentPage.php" method="post" >
<label for="playername"> Player </label>
<input type="text" name="addplayer" id="playername"/>
<input type= "submit" name="submit" value="submit" />
</form>

Upvotes: 0

D14n4
D14n4

Reputation: 130

<?php
    if (isset($_POST['submit'])) {
        $addplayerv=$_POST['addplayer'];
        $mysqli->select_db("player");

        $sql="INSERT INTO player (nameofplayer) VALUES ('".$addplayerv."')";
        $mysqli->query($sql);
        $mysqli->close();
    }
?>

select_db() needs one parameter: the name of the database you're using.
query() only needs the sql query.
close() doesn't expect any parameter.

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

missing submit input name your input should be something like this

<input type= "submit" value="submit" name="submit" />

and your db connection your passing two parameters in select_db() that's wrong you have to pass one parameter. should be like this

$player="player";

 $mysqli->select_db($player);

Upvotes: 0

Quentin
Quentin

Reputation: 943214

 if (isset($_POST['submit'])) {

You only process the data if the form submits a control named submit.

You have no control with that name (the submit button has submit as the type and the value but it has no name).

Give the submit button a name.

Upvotes: 3

Related Questions