php add songID in mysql - php

I have a table and I want to add songs into my database. Every song has its own songID , so when I try to add a song it also has to update the songID. at this moment it didn't work.
if($action == "add-songs"){
$db = mysqli_connect($host, $user, $pass,$database);
if($_GET['action3'] == "2"){
mysqli_query($db, "INSERT INTO songs (id, songName, songID) VALUES (0, '".$_GET['songname']."', '".$_GET['id']."')");
header("Location: /?action=show-songs");
}
$h = "";
$h.= "";
$h.= "<form><input type='hidden' name='action' value='add-songs'><input type='hidden' name='action3' value='2'><input type='hidden' name='id' value='id'><table class='table table-striped'>";
$h.= " <tr>";
$h.= " <td><b>Nummer</b></td>";
$h.= " <td><input type='text' name='songname' class='form-control' placeholder='Naam'></td>";
$h.= " </tr>";
$h.= " <tr>";
$h.= " <td colspan='2'><input class='btn btn-primary' type='submit' value='UPDATE'></td>";
$h.= " </tr>";
$h.= "</table></form>";
echo $htop;
echo $h;
echo $hbot;

The problem is because of the following <input> element,
<input type='hidden' name='id' value='id'>
You're using the same string value id as id column value for all of your songs.
Now look at your songs table structure, id column is of type INT, which means you're inserting a string value in the column of type integer. And this is why your business logic is failing.
The solution is, don't make the above input element hidden. Explicitly put the song id corresponding to the particular song name. So change the above <input> element like this:
<input type='text' name='id' placeholder='id'>

Related

How 2 change 'X' col. value of table on one click if each row of table represent in separate HTML forms with each submit button to change 'X' value?

I have mysql employee table with cols = (EmpID, ProjectCode, Date, StartTime, EndTime, NoOfHours, TaskPerformed, ModifiedDate, Status), Status consists of integer value either 1 (Billed) or 2 (Not billed) or 3 (Invoiced) or 4 (Uninvoiced), this status column values can be changed by the admin only.
so my problem is i am displaying each row values of table as different html form in PHP script with checkbox for status value where admin make a check on relevant i.e.particular entry should be billed or unbilled or invoiced or uninvoiced entry on reading the row values of employee table. but here problem comes on checking i need to make a post request for each form means i need to click on 10 buttons if i have 10 rows in a tables button i need to get the single button to submit all 10 form data.
please give direction.
below is if condition in admindashboardentry.php for matching if condition for retrieving table row values when the admin has specific selection for EmpID, ProjectCode,StartDate, EndDate i.e. admin provides values from a HTML Form in admindashboard.php via method POST to these variable to get table rows from database.
if(isset($_POST['empidcheck']) and isset($_POST['projectcodecheck']) and isset($_POST['startdatecheck']) and isset($_POST['enddatecheck'])){
$empid = $_POST['empid'];
$projectcode = $_POST['projectcode'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
//to make database connection
include('connection.php');
echo "<p>Admin Entry Received: $empid, $projectcode, $startdate, $enddate</p>";
//To Display Name and Employee ID above table Start
$nameDisplaySql = "SELECT EmpID, Name FROM employee_data WHERE EmpID = '$empid'";
$result = $conn->query($nameDisplaySql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<p>".$row["EmpID"]." - ".$row["Name"]."</p>";
echo "<hr>";
}
}
//To Display Name and Employee ID above table End
$selectSql = "SELECT * FROM $empid WHERE EmpID = '$empid' AND ProjectCode = '$projectcode' AND Date BETWEEN '$startdate' AND '$enddate' ORDER BY Date DESC, ModifiedDate DESC";
$result = mysqli_query($conn, $selectSql);
$sumNoOfHours = 0.0;
displayTableHeading();
while($row = mysqli_fetch_array($result))
{
$sumNoOfHours = $sumNoOfHours + $row['NoOfHours'];
displayTableRow($row);
}
tableEnding($sumNoOfHours);
//form element to make the button below table to get the entry
echo "<form action = 'admindashboardentry.php' method = 'post'>";
echo "<input type ='hidden' name='empid' value= '$empid' >";
echo "<input type ='hidden' name='projectcode' value= '$projectcode' >";
echo "<input type ='hidden' name='startdate' value= '$startdate' >";
echo "<input type ='hidden' name='enddate' value= '$enddate' >";
echo "<input type='submit' name = 'allsetdownload' value = 'Download'>";
echo "</form>";
}
Below are the functions for display row and heading to be used in displaying the rows of table. They are also in file admindashboardentry.php.
//Webpage Table Heading
function displayTableHeading(){
// echo "<div class='heading'>";
echo "<b>";
echo "<input type='text' size='7' value = 'EmpID' readonly>";
echo "<input type='text' style='margin-left:2px;' size='8' value = 'Project Code' readonly>";
echo "<input type='text' style='margin-left:2px;' size='6' value = 'Date' readonly>";
echo "<input type='text' style='margin-left:2px;' size='7' value = 'Start Time' readonly>";
echo "<input type='text' style='margin-left:2px;' size='7' value = 'End Time' readonly>";
echo "<input type='text' style='margin-left:2px;' size='8' value = 'No Of Hours' readonly>";
echo "<input type='text' style='margin-left:2px;' size='17' value = 'Task Performed' readonly>";
echo "<input type='text' style='margin-left:2px;' size='14' value = 'Modified Date' readonly>";
echo "</b>";
// echo "</div>";
}
//Webpage Table Row
function displayTableRow($row){
$taskstr = $row['TaskPerformed'];
$modifieddate = $row['ModifiedDate'];
echo "<form action='admindashboardentry.php' method = 'post'>";
echo "<input type='text' style='margin-top:2px;' name='empid' size='8' value =".$row['EmpID']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='projectcode' size='8' value =".$row['ProjectCode']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='date' size='8' value =".$row['Date']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='stime' size='8' value =".$row['StartTime']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='etime' size='8' value =".$row['EndTime']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='hours' size='8' value =".$row['NoOfHours']." readonly>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='taskperformed' size='20' value = '$taskstr'>";
echo "<input type='text' style='margin-top:2px; margin-left:2px;' name='modifieddate' size='15' value = '$modifieddate' readonly>";
echo "<span>&nbsp</span><input id='checkBox1' style='margin-top:2px; margin-left:2px;' type='checkbox' name='billed' > <span style='font-weight:bold;'>Billed</span> ";
echo "<span>&nbsp</span><input id='checkBox2' style='margin-top:2px; margin-left:2px;' type='checkbox' name= 'notbilled'> <span style='font-weight:bold;'>Not Billed</span>";
echo "<span>&nbsp</span><input id='checkBox3' style='margin-top:2px; margin-left:2px;' type='checkbox' name='invoiced'> <span style='font-weight:bold;'>Invoiced</span>";
echo "<span>&nbsp</span><input id='checkBox4' style='margin-top:2px; margin-left:2px;' type='checkbox' name='uninvoiced'> <span style='font-weight:bold;'>Uninvoiced</span>";
echo "<span>&nbsp</span><input type='submit' id='button' style='margin-top:2px; margin-left:2px; ' name = 'submit' style='margin-left:10px;'>";
echo "</form>";
}

HTML: putting a table in a form

I'm coding an html table that displays information from a MySql table. Each row is a series of input's so the values of the table can be easily updated.
Here's my current code:
<form action=index.php/component/studentmanagement/?task=update method=post>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Program</th>
<th>Class</th>
</tr>
<?php
$db = JFactory::getDBO();
$query = "SELECT * FROM student_management_module";
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as &$row) {
echo "<tr>";
echo "<td>" . "<input type=text name=fullName id=name_val value=" .$row->name. "> </td>";
echo "<td>" . "<input type=text name=email id=email_val value=" .$row->email. "> </td>";
echo "<td>" . "<input type=text name=prog id=prog_val value=" .$row->program. "> </td>";
echo "<td>" . "<input type=text name=class id=class_val value=" .$row->class. "> </td>";
echo "<td class = 'headcol'> <input type=submit name=update class='btnupdate' value=update>";
echo "<td>" . "<input type=hidden name=hidden value=" .$row->student_id. "> </td>";
echo "</tr>";
}
?>
</table> </form>
But whenever I try submitting the updated values, they don't get pass to my update functions. Am I putting the table in the form correctly?
Thanks in advance, and I'll appreciate any help.
This is my update function:
<?php
$db = JFactory::getDBO();
$query = "UPDATE student_management_module SET name = '$_POST[fullName]', email = '$_POST[email]', program='$_POST[prog]', class='$_POST[class]' WHERE student_id='$_POST[hidden]'";
$db->setQuery($query);
$db->query();
?>
Try having the value of the name and id in quotes or double quote. I.e., id="class_val". In your case, since you have already inserted the td in a double quote, use single quote. So it will be "<td class='foo' id='foo' name='foo'></td>".

php: Using <form> tag to sum a new value to an existing value in php

I am creating a webpage that is similar to a points system. It consists of a table with name and points columns. The user inputs a number, which then adds that value to the existing number in the table. My question is how would I be able to add those two values and update the table(database)?
<?php
$con = mysql_connect("xxx, xxx, xxx);
if (!$con) {
die("can not connect:" . mysql_error());
}
mysql_select_db("points", $con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE coach_tbl set coachscore = coachscore + '$add' WHERE coach_score = '$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if (isset($_POST['submit'])){
$AddQuery = "INSERT INTO coach_tbl (coach_name, coach_score) VALUES('$_POST[name]', '$_POST[score]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM coach_tbl ORDER BY coach_score DESC";
echo "<table border=1>
<tr>
<th>NAME</th>
<th>Score</th>
</tr>";
$myData = mysql_query($sql, $con);
while($record = mysql_fetch_array($myData)) {
echo "<form action=index.php method=post>";
echo "<tr>";
echo "<td><input type=text name=coachname value='" . $record['coach_name'] . "'> </td>";
echo "<td><input type=text name=coachscore value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=hidden name=hidden value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=submit name=update value=update'" . "'> </td>";
echo "<td><input type=number min="1" max="10" name=add value=add'" . "'> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
If there are any questions I will gladly elaborate on anything. I am also fairly new to php.
Let's say you have an html form like so:
<form action="update.php" method="POST">
<input type="number" name="updateData" value=""/>
<input type="submit">
</form>
and an update.php:
<?php
//assuming you want 1-10 points max to be added each time
if( isset($_POST['updateData']) && $_POST['updateData'] >=1 && $_POST['updateData'] >=10){
//set to user inputed value
$insertData = $_POST['updateData'];
$sql = "UPDATE point_table SET points = points + $insertData WHERE id = 1";
//you will need to finish off the query by checking connecting with your database.
}
?>

how to display four radio buttons dynamically inside a dyanamically created html table?

this my table code
$mem=mysql_query("SELECT * FROM tbl_user_profile WHERE comp_id= $cmp");
while($result=mysql_fetch_array($mem))
{
echo"<tr>" ;
echo"<td><font color='red' size=4> ".$result['fname']." </font></td>";
echo"<td><font color='red' size=4 >".$result['emp_id']."</font></td>";
inside this table in the third column i tried while loop to show radio buttons.
$query = "SELECT * FROM tbl_house where event_id = $eid";
$hid = mysql_query($query);
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<td> . <input type=\"checkbox\" name=\"q1\" value=\"$house_id\" />$house_name </td>";
}
echo "<td> <a href='house_member.php?operation=dele&id=".$result['profile_id']."'><input type='button' value='add' id='btn'></a></td>";
echo"</tr>" ;
echo"</table>";
}
its not working properly. somebody please help. i'm just a beginner in php.
waiting for a better solution.
if you want them all to be in the same column, don't put <td> around each of them, do that outside the loop. And the type of the input should be radio, not checkbox.
echo "<td>";
while ($row = mysql_fetch_array($hid)) {
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id'/>$house_name ";
}
echo "</td>";
please change the <Input type="checkbox"> to <input type "radio">
and
echo "<input type='radio' name='q1' value='$house_id'/>$house_name "; this is the right way to display html code into php
Are you trying to add radio buttons in the third column or do you want to create a column for each radio??
my guess is that you remove the from within the while loop and place the starting and ending tags outside the while loop. something like
<td>
while loop
</td>
hope this helps
You have to use the following code at the place of your while loop.....
<td>
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id' />$house_name ";
}
</td>

Create dropdown list from another table into my update/delete/add table

Im a newbie and working on a project for school
I have a website that lists foods.
I have an update table that allows me to change and add data.
For the food group field I have it cross reference another table called food_group which has the food_group(name) and an id.
When you view the food data you can see the name that it pulls instead of the ID. On the update page I would like a drop down to be in the place of the ID. So you can see the "friendly" name instead of the ID number, but it has to store the ID not the friendly name in the food table.
Website can be found at http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
The code I have is:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);
echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
____________ Update 12/2/13 10:30pm ___________________
Ok so if I create a new php page like the following it will work. However, I have no idea how to combine it into the original above... Can anyone help?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
Thank you for all your help!
Jason
Your script is nice but I just want to point the following:
There's no need to concatenate this
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
you can type it like this:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
also you missed to close your input tag
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
and another is you need to quote your attribute values , see below
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
Last thing is that you're open to SQL injection, so you should start learning mysqli and prepared statement

Categories