Sinisterly
Help with Updating database from PHP? - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: Help with Updating database from PHP? (/Thread-Help-with-Updating-database-from-PHP)

Pages: 1 2 3


Help with Updating database from PHP? - mudrig - 07-27-2016

Im still in the process of learning PHP. Here is what im trying to accomplish. I have a database table named users with 4 columns. user_name, user_rank, user_tag, list_order. I want the user to be able to select the user_name from the form and enter the value for the other 3 columns and it update into the database on the selected users row. This is what I have and the error I am getting. How do I go about fixing this.

error:
Fatal error: Call to a member function bind_param() on a non-object in /hermes/bosnaweb24a/b1321/ipg.darkgaminglivecom/panel/user_rank_change.php on line 17

user_change.php:
Code:
<form method='post' action='user_rank_change.php'> <!-- Gamertag Select --> <h3 for='select_gamertag' >Select GamerTag</h3> <select class="form-control" id='select_gamertag' type='text' name='select_gamertag'> <? while($row = mysqli_fetch_array($result6)) { echo "<option>" . $row['user_name'] . "</option>"; } ?> </select> <!-- Select Rank --> <h3 for='select_rank' >Type rank as it is shown below</h3> <input id='select_rank' type='text' name='select_rank' class="form-control" placeholder="Example: Recruit"> <!-- Select Tag --> <h3 for='select_tag' >Type tag as it is shown below</h3> <input id='select_tag' type='text' name='select_tag' class="form-control" placeholder="Example: [DSxR]"> <!-- Select Order --> <h3 for='select_order' >Type order as it is shown below</h3> <input id='select_order' type='text' name='select_order' class="form-control" placeholder="Example: 27"> <!-- Submit Form --> <button type="submit" class="btn btn-default">Change Rank</button> </form>

user_rank_change.php:
Code:
// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // prepare and bind $stmt = $conn->prepare("UPDATE users (user_rank, user_tag, list_order) VALUES (?, ?, ?) WHERE user_name = '$gamertag'"); $stmt->bind_param("sss", $_POST['select_rank'], $_POST['select_tag'], $_POST['select_order']); // set parameters and execute $gamertag = $_POST['select_gamertag']; $rank = $_POST['select_rank']; $tag = $_POST['select_tag']; $order = $_POST['select_order']; $stmt->execute(); echo "New records created successfully"; $stmt->close(); $conn->close(); ?>



RE: Help with Updating database from PHP? - Ex094 - 07-27-2016

Your error is probably generating from the Prepare() function as your UPDATE statement seems to be incorrect, the sytanx you wrote is for INSERT as for UPDATE it's like
Code:
UPDATE users SET user_rank = something, user_tag = something, list_order = something WHERE user_name = '$gamertag'



RE: Help with Updating database from PHP? - mudrig - 07-27-2016

(07-27-2016, 07:59 PM)Ex094 Wrote: Your error is probably generating from the Prepare() function as your UPDATE statement seems to be incorrect, the sytanx you wrote is for INSERT as for UPDATE it's like
Code:
UPDATE users SET user_rank = something, user_tag = something, list_order = something WHERE user_name = '$gamertag'

I did it like this and now Im getting this error.

Error:
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /hermes/bosnaweb24a/b1321/ipg.darkgaminglivecom/panel/user_rank_change.php on line 17 New records created successfully

Code:
// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // prepare and bind $stmt = $conn->prepare("UPDATE users SET user_rank = '$rank', user_tag = '$tag', list_order = '$order' WHERE user_name = '$gamertag'"); $stmt->bind_param("sss", $_POST['select_rank'], $_POST['select_tag'], $_POST['select_order']); // set parameters and execute $gamertag = $_POST['select_gamertag']; $rank = $_POST['select_rank']; $tag = $_POST['select_tag']; $order = $_POST['select_order']; $stmt->execute(); echo "New records created successfully"; $stmt->close(); $conn->close(); ?>



RE: Help with Updating database from PHP? - Ex094 - 07-27-2016

Query should be like:
Code:
UPDATE users SET user_rank =?, user_tag =?, list_order =? WHERE user_name =?

And the Bind_Param
Code:
$stmt->bind_param("ssss", $_POST['select_rank'], $_POST['select_tag'], $_POST['select_order'], $_POST['select_order']);



RE: Help with Updating database from PHP? - mudrig - 07-27-2016

Thanks that fixed that problem. Now the problem I am having is it is not updating it in the database.


RE: Help with Updating database from PHP? - Ex094 - 07-27-2016

Are you giving a correct User Name as the input? Do keep case sensitivity in mind

EDIT: Also check the data types of the Columns are correct, i.e. they are all string, if anyone of them is an int, then use 'd' instead of an 's' in the bind_param


RE: Help with Updating database from PHP? - mudrig - 07-27-2016

Yes I have the usernames being pulled straight from the database. In the form above.

Code:
<form method='post' action='user_rank_change.php'> <!-- Gamertag Select --> <h3 for='select_gamertag' >Select GamerTag</h3> <select class="form-control" id='select_gamertag' type='text' name='select_gamertag'> <? while($row = mysqli_fetch_array($result6)) { echo "<option>" . $row['user_name'] . "</option>"; } ?> </select>



RE: Help with Updating database from PHP? - Ex094 - 07-27-2016

Try adding this line
Code:
if(!$stmt->execute()) { echo $stmt->error; } else { echo "New records created successfully"; }

Instead of
Code:
$stmt->execute(); echo "New records created successfully";

See if any errors come up


RE: Help with Updating database from PHP? - mudrig - 07-27-2016

(07-27-2016, 08:59 PM)Ex094 Wrote: Try adding this line
Code:
if(!$stmt->execute()) {      echo $stmt->error; } else {     echo "New records created successfully"; }

Instead of
Code:
$stmt->execute(); echo "New records created successfully";

See if any errors come up

Okay so I changed it over to that and the user_rank_change.php is executing correctly. Its almost as if its not pulling the values over from the form.


RE: Help with Updating database from PHP? - Ex094 - 07-27-2016

Oh jeez, I think we got it. You forgot to add the Value attribute to the OPTION tags hence it's returning gamer_tag null I think

Replace this line:
Code:
echo "<option>" . $row['user_name'] . "</option>";

With this:
Code:
echo "<option value='" . $row['user_name'] . "'>" . $row['user_name'] . "</option>";

Also just in case to make sure inputs are coming correctly, Echo out all the POST variables like:
Code:
echo $gamertag; echo $rank; echo $tag; echo $order;