Can't save table information to MySQL - php

I have been working with this code for a whole day but still can't understand where I made a mistake. So basically I want to edit whole mysql table.
I'm easily fetching it, and I need to change some of the data and save it again to the table. But everytime I'm trying to save it - everywhere getting "Array" and nothing more.
It's look like the code is not seeing that I'm making changes there.
Also sometimes when I'm changing bits, only last raw was saving and duplicating everywhere after.
<form method="POST" action="index.php">
<table><thead><tr><th>ID</th><th>Name</th><th>Live</th><th>AQ</th><th>Up</th><th>Down</th><th>Cptch</th><th>VID</th><th>UpOrDo</th><th>UTool</th><th>STool</th></tr><thead><tbody>
<?php
while ($row = $result->fetch_array()) {
echo "<tr><td><input size='1' name='id[]' value='".$row["ID"]."' readonly></td><td>".$row["Name"]."</td><td>".$row["Live"]."</td><td>".$row["AccQuantity"]."</td><td>".$row["Upvote"]."</td><td>".$row["Downvote"]."</td><td>".$row["Captcha"]."</td><td><input name='VoteID[]' type='text' size='5' value='".$row["VoteID"]."'></td><td><input name='UpOrDown[]' type='text' size='5' value='".$row["UpOrDown"]."'></td><td><input name='UpvoteTool[]' type='text' size='5' value='".$row["UpvoteTool"]."'></td><td><input name='SignupTool[]' type='text' size='5' value='".$row["SignupTool"]."'></td></tr>";
}
?>
</tbody>
</table>
<input type='submit' name='update' value='UPDATE' />
</form>
</body>
</html>
<?php
if(isset($_POST['update'])){
$ID = $_POST['id'];
$VoteID = $_POST['VoteID'];
$UpOrDown = $_POST['UpOrDown'];
$UpvoteTool = $_POST['UpvoteTool'];
$SignupTool = $_POST['SignupTool'];
for($i = 1; $i < 7; $i++) {
$user_id = $ID[$i];
$sql = "UPDATE Servers SET `VoteID`='".$VoteID."', `UpOrDown`='".$UpOrDown."', `UpvoteTool`='".$UpvoteTool."', `SignupTool`='".$SignupTool."' WHERE `ID`=".$user_id."";
echo $sql."<br>";
}
}

It seems you are not running the query. Also you are not picking out the data from the array. I would do this:
for($i = 1; $i < 7; $i++) {
$user_id = $ID[$i];
$_VoteID = $VoteID[$i];
$_UpOrDown= $UpOrDown[$i];
$_UpvoteTool= $UpvoteTool[$i];
$_SignupTool= $SignupTool[$i];
$sql = "UPDATE Servers SET `VoteID`='".$_VoteID."', `UpOrDown`='".$_UpOrDown."', `UpvoteTool`='".$_UpvoteTool."', `SignupTool`='".$_SignupTool."' WHERE `ID`=".$user_id."";
mysqli_query($sql);//or whatever way you are runing your query.
echo $sql."<br>";
}

Related

Independent radiobuttons for each row read from server

I'm currently trying to generate independent radio buttons for each row read from my server. They are supposed to symbolize ON and OFF Switches. My current issue is that I manage to generate buttons for each row but these are not independent. I can only turn one off or on at a time.
I know that radio buttons become truly "independent" when the name of each is different. But I can't seem to figure out a way to create a new name for each row generated.
Currently my code looks like this
res = $db->query($query);
if ($res->num_rows > 0) {
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton' id='myCheck'></td>
<td><input type='radio' name='radiobutton' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
}
}
echo $content;
I've been trying different methods such as using <td><input type='radio' name='$row->id' id='myCheck'></td>
But have not gotten it to work. Any help on how to solve this issue is greatly appreciated.
Thank you!
If you want the unique names for all the checkboxes generated you can have them by just incrementing $i for each row.
res = $db->query($query);
if ($res->num_rows > 0) {
$i = 0;
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck'></td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
$i++;
}
}
echo $content;
If you do not care about the name of the radio button, you could use a "php random number generator", each row you asign a new random number to your radio button name:
<?php $x = rand(5, 100000); ?> <input type='radio' name='<?php echo $x; ?>' id='myCheck'>

How to set a maximum where value=$sum

I have an option for users to give away point to other users
I would like to make sure that the points they give away don't exceed the points they have.
How do I do this?
maxvalue doesn't work.
<?php
$user=$_SESSION['SESS_USERID'];
if(isset($_REQUEST['user'])){
$user = preg_replace('#[^a-z0-9]#i', '', $_GET['user']);
}
$sql=mysqli_query($conn,"SELECT SUM(sterren) AS total FROM sterren WHERE userid='$user'");
if ($row = mysqli_fetch_assoc ($sql)){
$sum = $row['total'];
if ($row["total"] >= '1') {
echo"
<table>
<some form action>
///Some more code to select the user to give the points to
<input type='text' name='sterren' id='sterren' size='3' maxlength='3' maxvalue=$sum ></td>
</form>
</table>";
}
}
<?
I think you should use the 'number' attribute instead of 'text'.
Your syntax should be something like this:
<input type='number' name='sterren' id='sterren' min='0' max='<?php echo $sum;?>'>
I think the synatx should be like the following
<input type='text' name='sterren' id='sterren' size='3' min="1" max="5">
Try max instead of maxvalue as the input attribute
References: http://www.w3schools.com/tags/att_input_max.asp
Demo: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_max_min

More than one htmlspecialchars string in PHP MySQL

I have a table in MySQL where there is a row with this data.
id = 187
friendly name = i don't like mustard
filetype = exe
This first block of code below works perfectly, and echos text i don't like mustard into an HTML form. Similarly, if I change $row['friendlyname'] to $row['filetype'], text exe is echoed. All good, no issues yet.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
The problem I'm having is if I try to echo both $row['friendlyname'] and $row['filetype'], only the variable that is listed first will be echoed. For example, in the below code, $row['friendlyname'] is listed before $row['filetype']. In this example, only $row['friendlyname'] (i don't like mustard) will be echoed. Similarly, if $row['filetype'] is listed before $row['friendlyname'], then only $row['filetype'] (exe) is echoed, and the second other HTML input form is empty.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
<input type='text' value='<?php echo $filetype; ?>'>
Note 1: It doesn't matter the order of the input type forms. I ruled that out as the issue.
Note 2: If I were to replace $row['friendlyname'] and $row['filetype'] with the text I'm trying to echo, then it work (the below code). So, this definitely appears to be something with these $row variables.
<?php
$con = mysqli_connect('domain','user','pass','db');
$sql = "select * from installers where id=187";
$result = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result))
$friendlyname = i don't like mustard;
$filetype= exe;
$con->close();
?>
<input type='text' value='<?php echo $friendlyname; ?>'>
<input type='text' value='<?php echo $filetype; ?>'>
You have not added brackets into while loop so only first record is populated.
This block:
while($row=mysqli_fetch_array($result))
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
Should be:
while($row=mysqli_fetch_array($result)){
$friendlyname = htmlspecialchars(" ".$row['friendlyname']." ",ENT_QUOTES);
$filetype= htmlspecialchars(" ".$row['filetype']." ",ENT_QUOTES);
}

PHP - Getting back Post from forEach select form

i have a website where the admin can choose to add a certain number of bbcode tags and the corresponding html tags.
First he chooses how many tags he wants to add from a drop down select form in a for Each loop.
Depending on how many tags he chose when he clicked the submit button, the corresponding number of input tags appear in a second form, also in a for Each loop. He fills in the bbcode and html input and clicks the submit button. Normally the tags should be added to my database but in this case when he clicks submit the form disappears and nothing is added..
Here is the code :
//FIRST FORM WHERE HE DECIDES HOW MANY TAGS TO ADD
<form id='nbbalises' action='config.ini.php' method='post' accept-charset='UTF-8'>
<fieldset>
<legend>How many tags ?</legend>
<input type='hidden' name='submitted' id='submitted' value='1' />
<?php
echo '<select name="number">';
$range = range(1,50,1);
foreach ($range as $nb) {
echo "<option value='$nb'>$nb</option>";
}
echo "</select>";
?>
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form><br /> <br />
<?php
if (!(empty($_POST['number']))) {
if ($_POST['number'] >= 1 && $_POST['number']<= 50){
$number = $_POST['number'];
$range2 = range(1,$number,1);
?>
//SECOND FORM WHERE I GENERATE THE INPUT DEPENDING ON THE NUMBER CHOSEN FROM FIRST FORM
<form id='balises' action='config.ini.php' method='post' accept-charset='UTF-8'>
<fieldset>
<legend>Balises bbc : </legend>
<input type='hidden' name='submitted' id='submitted' value='1' />
<?php
foreach ($range2 as $nb2) {
echo "<label>bbcode tag $nb2 :</label>
<input type='text' size='40' name='bbc$nb2' id='bbc$nb2' maxlength='40' />
<label>html tag $nb2 :</label>
<input type='text' size='40' name='html$nb2' id='html$nb2' maxlength='40' />
<br />";
}
}
?>
<input type='submit' name='Submit2' value='Submit2' />
</fieldset>
</form>
<?php
//PROBLEM STARTS HERE, NOTHING WORKS UNDER HERE
if (isset($_POST['Submit2'])){
//CONNECT TO MY DATABASE
connectDB();
for ($i=0; $i<$number ; $i++){
if (!(empty($_POST["bbc$i"])) && (empty($_POST["html$i"])))
//FUNCTION ADDS TAGS TO DATABASE
addBbc($_POST["bbc$i"], $_POST["html$i"]);
}
mysql_close();
}
}
//MY FUNCTIONS TO ADD THE BBCODE AND HTML TO DATABASE
function connectDB(){
//connexion DB
$link = mysql_connect('127.0.0.1', 'USERNAME', 'PASSWORD');
if (!$link) {
die('Erreur de connexion: ' . mysql_error());
}
$db = mysql_select_db('1213he200967',$link) or die("N'a pu selectionner
1213he200967");
mysql_query("SET NAMES 'utf8'");
}
function addBbc($bbc, $html){
$b = mysql_real_escape_string($bbc);
$h = mysql_real_escape_string($html);
$query="INSERT INTO bbcode (BBC,HTML) VALUES ('$b','$h')";
$result = mysql_query($query) or die("error");
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
die($message);
return false;
}
else return true;
}
Thank you very much, and sorry if my code is amateur-ish, i'm only starting in php.
EDIT
Found part of my problem
$number = $_POST['number'];
$range2 = range(1,$number,1);
This goes from 1 to the number chosen by the user in the first form.
for ($i=0; $i<$number ; $i++){
if (!(empty($_POST["bbc$i"])) && (empty($_POST["html$i"])))
//FUNCTION ADDS TAGS TO DATABASE
addBbc($_POST["bbc$i"], $_POST["html$i"]);
This goes from 0 to $number - 1
So i changed my code to this.
for ($i=0; $i<$number ; $i++){
$nb = $i + 1;
if (!(empty($_POST["bbc$nb"])) && (empty($_POST["html$nb"]))) {
addBbc($_POST["bbc$nb"], $_POST["html$nb"]);
}
else echo "$nb tags empty ";
This works a bit better but now it goes to the else just here above and displays "2 tags empty", so it still doesn't quite work.
Ok to solve it I finally decided to post the data from the second form to another page and i modified my code to this.
for ($i=0; $i<$number ; $i++){
$nb = $i + 1;
$varBbc = 'bbc'.$nb;
$varHtml = 'html'.$nb;
if ((isset($_POST[$varBbc])) && (isset($_POST[$varHtml]))) {
addBbc($varBbc, $varHtml);
}
else echo "$nb tags empty ";
}
Apparently using !empty instead of isset doesn't work in this case, maybe because of the html tags.
$nb should be in double quotes.
so echo "<option value='$nb'>$nb</option>"
change to echo "<option value='".$nb."'>$nb</option>";
and also
if (!(empty($_POST['number']))) {
if ($_POST['number'] >= 1 && $_POST['number']<= 50){
$number = $_POST['number'];
$range2 = range(1,$number,1);
?>
change to:
if (isset($_POST['submit'])) {
if ($_POST['number'] >= 1 && $_POST['number']<= 50){
$number = $_POST['number'];
$range2 = range(1,$number,1);
?>

how to save runtime created table values into database

I create one form when i enter number of Rows & Columns then that number of rows & Column table would be generated i want save values entered into that table into database.Please anyone can help me..
My PHP CODE:
<?php
global $Hostname;
global $Username;
global $Password;
global $Database_name;
function getConnection()
{
$Hostname = "localhost";
$Username ="root";
$Password ="";
$Database_name="labdata";
$oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name);
return($oMysqli);
}
if(isset($_POST['submit']))
{
echo "<table border='1' align='center'>";
for($iii = 0;$iii <$_POST['column'];$iii++)
{
echo "<tr>".$jjj."</tr>";
for($jjj = 0; $jjj <$_POST['rows'];$jjj++) //loop for display no. of rows.
{
echo "<td>" ."<input type=\"text\" name='$iii'>"."</td>";
}
}
echo "</table>";
echo"<form name=\"aa\" method=\"post\">";
echo "<input type=\"submit\" name=\"save\" value=\"save\">";
echo "</form>";
}
$TestName = $_POST['testname'];
$Result = $_POST['result'];
$Unit = $_POST['unit'];
$NormalRange = $_POST['NormalRange'];
if(isset($_POST['save']))
{
$InsertQuery = "INSERT INTO rct(testname,result,unit,NormalRange) VALUES('$TestName','$Result','$Unit','$NormalRange')";
$oMysqli= getConnection();
$oMysqli->query($InsertQuery);
print_r($InsertQuery);exit();
while($Row = $InsertQuery->fetch_array())
{
$TestName = $Row['testname'];
$Result = $Row['result'];
$Unit = $Row['unit'];
$NormalRange = $Row['NormalRange'];
}
}
?>
<html>
<head>
<title>Rct</title>
</head>
<body>
<form name='abc' method="post">
<label for='Table'>Define Table</label>
<label for='rows'>Row</label>
<input type="text" name="column"></input>
<label for='column'>Column</label>
<input type="text" name="rows"></input>
<input type="submit" name="submit" value="submit" onclick="aaa()">
</form>
</body>
</html>
There are many problems with your code:
The table containing <input/>s should be inside <form name='aa'>.
The inputs that are in the table should be named something like $iii[].
The [] tells PHP to create an array of all the rows in $_POST, so you can access $_POST[0][0] for row 0, col 0, $_POST[1][2] for row 2, col 1, etc.
You seem to have rows and columns backwards.
All your <input/> tags are missing the /.
There's no form with inputs named testname, result, unit, NormalRange, so why are you accessing these $_POST values?
fetch_array() can only be used after a SELECT query. INSERT doesn't return any values.
Probably other things I missed.
First of all you must correct the input tag
Replace
<input type="text" name="column"></input>
To
<input type="text" name="column" />
Second the upper loop must be row loop instead of colum as column sit inside the row
if(isset($_POST['submit']))
{
echo"<form name=\"aa\" method=\"post\">";
echo "<table border='1' align='center'>";
for($iii = 0;$iii <$_POST['rows'];$iii++)
{
echo "<tr>";//start Row here
for($jjj = 0; $jjj <$_POST['column'];$jjj++) //loop for display no. of rows.
{
echo "<td>" ."<input type=\"text\" name='".$iii.$jjj."'>"."</td>";//all TDs must be inside the row
}
echo "</tr>";//end row here
}
echo "</table>";
echo "<input type=\"submit\" name=\"save\" value=\"save\">";
echo "</form>";
}
And of course table must be inside the form.
Let me know if this helps

Categories