Read data form mysql table - php

Hello everyone I am trying to write a PHP code that reads the information from my database, unfortunately I got the following error message:
Notice: Undefined index: numcontrat in C:\wamp\www\test\crud-php-simple-master\crud-php-simple-master\index.php on line 46 and
Notice: Undefined index: dnotification in C:\wamp\www\test\crud-php-simple-master\crud-php-simple-master\index.php on line 51
I share with you my source code:
<?php`
//including the database connection file
include_once("config.php");
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
Add New Data<br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td style="background-color:#57D251; "> <b> N Contrat</b></td>
<td style="background-color:#57D251; " ><b> Avenant</b></td>
<td style="background-color:#57D251; " ><b> Discription</b></td>
<td style="background-color:#57D251; " ><b> Fournisseur</b></td>
<td style="background-color:#57D251; " ><b> Delai</b></td>
<td style="background-color:#57D251; " ><b> Date de notification</b></td>
<td style="background-color:#57D251; " ><b> Date envigyeur</b></td>
<td style="background-color:#57D251; " ><b> Date de fin caution</b></td>
<td style="background-color:#57D251; " ><b> Avance forfaitaire</b></td>
</tr>
<?php
//fetching data in descending order (lastest entry first)
$sql= 'SELECT * FROM contrats ';
$result = mysql_query($sql);
if (! $result){
echo('Database error: ' . mysql_error());
}
echo $result;
while($res = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" .$res['numcontrat']."</td>" ;
echo "<td>" .$res['avenant']."</td>" ;
echo "<td>" .$res['discription']."</td>" ;
echo "<td>" .$res['fournisseur']."</td>" ;
echo "<td>" .$res['delai']."</td>" ;
echo "<td>" .$res['dnotification']."</td>" ;
echo "<td>" .$res['denvigyeur']."</td>" ;
echo "<td>" .$res['dcaution']."</td>" ;
echo"<td>" .$res['aforfaitaire']."</td>" ;
echo "<tr>";
//echo "<td>Edit | Delete</td>";
}
?>
</table>
</body>
</html>
thank you for your answer

According to your exception, dnotification column doesn't exist in your table contrats.
Create this column in your Database table and then try.
In your Comment you have mentioned
//fetching data in descending order (lastest entry first)
$sql= 'SELECT * FROM contrats ';
But it will fetch in the order it is stored.To fetch in descending order, Use query something like this...
SELECT * FROM contrats order by id DESC
where id is the autoincremented field.

Related

Getting a specific row from PHP database to add to a new empty database upon clicking an "add to playlist button"

I have a group project for my web programming class, we are creating a simple music website and have created a database using phpmyadmin for each genre, we have a add to playlist button that should add the selected song to the empty database but we can not figure out why it is not working.
<div id="navbar">
<table>
<tr>
<td>
<font size="6"><b>Home</font>
</td>
<td>
<font size="6"><b>Pop</font>
</td>
<td>
<font size="6"><b>Hip-Hop</font>
</td>
<td>
<font size="6"><b>Rock</font>
</td>
<td>
<font size="6"><b>Country</font>
</td>
<td>
<font size="6"><b>Alternative</font>
</td>
<td>
<font size="6"><b>My Playlist</font>
</td>
</tr>
</table>
</div>
<!-- End of navbar div -->
<br>
<!-- PHP code here to display the appropriate databse table (genre/playlist) -->
<?php
$dbc = mysqli_connect("localhost", "root", "", "Music Website") or die("Bad Connect:" . mysqli_connect_error());
$sql = "SELECT * FROM AltMusic";
$result = mysqli_query($dbc, $sql) or die("Bad Query: $sql");
echo "<table id = 'database'>";
echo "<tr id = 'tableHead'>
<td class = 'titleCells'>Title</td>
<td class = 'titleCells'>Artist</td>
<td class = 'titleCells'>Album</td>
<td class = 'titleCells'>YouTube</td>
<td class = 'titleCells'>Genre</td>
<td class = 'titleCells'>Add to Playlist</td>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td class = 'cells'>{$row['Title']}</td>
<td class = 'cells'>{$row['Artist']}</td>
<td class = 'cells'>{$row['Album']}</td>
<td class = 'cells'><a href='" . $row['YouTube'] . "'target = _blank>" .
$row['YouTube'] . "</a></td>
<td class = 'cells'>{$row['Genre']}</td>
<td id = 'add' class = 'cells'>" ?>
<form action='' method='POST'>
<input type='submit' name='submit' value="+"/>
</form><?
if (isset($_POST['submit'])) {
$sql2 = "INSERT INTO Playlist(`ID`, `Title`, `Artist`, `Album`, `YouTube`,
`Genre`)
SELECT `ID`, `Title`, `Artist`, `Album`, `YouTube`, `Genre`FROM Pop
WHERE ";
}
echo "</td>
</tr>";
}
echo "</table>";
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<br> Title: " . $row['Title'] . " Artist: " . $row["Artist"] . " Album: " .
$row["Album"] . "YouTube: " . $row["YouTube"] . "Genre: " . $row["Genre"] . "Add to Playlist: " .
$row["Add to Playlist"] . "<br>";
}
} else {
echo "0 results";
}
$dbc->close();
?>
<!-- Bookmark to allow uesrs to return to the top of the page once at the bottom -->
<a href=#top>
<font size="6">Back to top</font>
</a>
I have tried to use isset($_POST("") to add it but it can only figure out how to fetch the entire array instead on one single selection.
Please consider that I, as well as all of the members in my groups are fairly new to web programming. If I have not posted correctly or not given specific enough ideas about what I am trying to do please let me know in a considerate way. Thank you for any help you can provide.

Group list of array in a table [duplicate]

This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 11 months ago.
I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...
But it's showing all the content in single column, How I can fix it?
It shows like that right now:
[![enter image description here][1]][1]
PHP:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links">« <a class="active" href="#">1</a> 2 3 4 »</div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
echo "<td>" . $row['id'] . "</td>";
echo "<tr><td>" . $row['username'] . "</td></tr>";
echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
</tbody>
</table>
[1]: https://i.stack.imgur.com/jPTR8.png
change while() loop code like below:
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>"; //put <tr> opening code inside, not outside
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "</tr>"; //</tr> need to be added at last
}
Try this
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?> </td>
</tr>
<?php
}
?>

Increment database by 1, using UPDATE (mysql). What's wrong with my code?

I try to write code that receive a student ID as an input in student_list.php, and pass this ID to score.php.
At score.php, use that ID to match and pull out student's name from database, and display it here.
Then, below the name, there is an input field, for adding 1 score to database for this student.
But I got this message, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1".
Any help would be appreciated. Thank you.
student_list.php
<?php
include_once 'DBconnect.php';
?>
<html>
<head>Student List</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> <th> Button </th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '<td> <button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button> </td>';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
score.php
<?php
include_once 'database_connect.php';
?>
<html>
<head>Add Score</head>
<body>
<?php
$student_id = $_POST["btn_student_id"];
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '</tr>';
}
echo "</table>";
if(isset($_POST['btn_add_score'])) {
$score = $_POST['score'];
mysql_query ("UPDATE term3 SET score = score + 1 WHERE student_id = ' ".$_POST['btn_student_id']. " ' ");
}
?>
<form method="post">
<table>
<tr>
<td>Score</td>
<td> <input type="number" name="score" size="8">
<button type="submit" name="btn_add_score" >Add</button>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------
Student ID | Name | Select |
----------------------------------------
10001 | Pat | Button (submit) |
----------------------------------------
10002 | Jess | Button (submit) |
----------------------------------------
Try to remove the quotation marks around the student id in the update query. It is redundant
UPDATE term3 SET score = score + 1 WHERE student_id = ".$_POST['btn_student_id']);
Update
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
to
$result = mysql_query("SELECT * FROM term3 WHERE student_id='".$_POST['btn_student_id']."'")

How can you update a query result performed by a query using php and mysql

I am trying to figure out how to update a "date" result from a query I perform.
Here is the code im using to perform the initial query:
<?php
include ("config.php");
$result = mysqli_query($con,"SELECT * FROM redmine.issues join redmine.projects ON issues.project_id=projects.id where projects.name='".$_SESSION['name']."';");
echo "<table border='0' width='80%' cellpadding='0' cellspacing='2' id='datatable' class='table table-bordered table-condensed table-hover table-striped'>
<tr>
<th width='50%' align='left'>Milestone</th>
<th width='25%' align='left'>Template Date</th>
<th width='15%' align='left'>Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='left'><input class='hidden' type='text' name='subject' id='subject' value='" . $row['subject'] . "'>" . $row['subject'] . "</td>";
echo "<td align='left'>" . $row['created_on'] . "</td>";
echo "<td align='left'><input type='text' name='due_date' id='due_date' value='".$row['due_date']."'></td>";
echo "<td align='left'><input type='submit' value='Submit New Date'></td>";
echo "</tr>";
}
echo "</table>";
echo "<br />";
mysqli_close($con);
?>
This will give me this result:
----------------------------------------
Milestone | Created Date | Due Date
----------------------------------------
Milestone 1 | 01/22/2015 | 01/22/2015
----------------------------------------
I want to be able to then type in a new date in the "Due Date" textbox for each milestone and update the due date in my mysql table for that milestone.
Any help is appreciated!
I now have this script.. but it only seems to update the last mysql query result. for example, if I have 5 milestones retrieved, I can only update the last mile stone date. If I try to update the other 4 milestone dates, they wont update.
<?php
include ("config.php");
// Get values from form
$subject=$_POST['subject'];
$due_date=$_POST['due_date'];
$result = mysqli_query($con,"UPDATE redmine.issues join redmine.projects ON issues.project_id=projects.id SET issues.due_date='$due_date' WHERE issues.subject='$subject' AND projects.name='".$_SESSION['name']."'");
// close connection
mysqli_close($con);
?>
Create a input[type="text"] field like this:
<input type="text" value="something" id="DueDate" data-id="12" onchange="function_update()"/>
In function_update() get current id by data-id="12" and value by id="DueDate" using ajax for onchange event.

get and display multiple checkbox values from database using php

i having two check boxes when i am selecting both check boxes i want to show the value related to both from database ,but now i able to print only one id of check boxes i want to show both data can anyone help hoe to show both a value ,Below is my code:
html
<input type="submit" name="Update" src="/image/exporttt.png" style="margin:0px;cursor:pointer">
<tbody>
<?php
$clientid=$_GET['clientid'];
if($clientid!=""){
$sql = mysql_query("SELECT * FROM clientnetworkpricehistory");
while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
{
echo '<tr class="alt">';
$alt = 0;
}
else
{
echo '<tr>';
$alt = 1;
}
echo '
<td id="CPH_GridView1_clientid" style="width:140px" class=" clientid '.$rows["net_id"].'">'.$rows["clientid"].'</td>
<td id="CPH_GridView1_country" style="width:160px" class=" country '.$rows['net_id'].'">'.$rows["country"].'</td>
<td id="CPH_GridView1_networkname" style="width:156px" class="networkname '.$rows["net_id"].'">'.$rows["networkname"].'</td>
<td id="CPH_GridView1_mccmnc" style="width:250px" class=" mcc'.$rows["net_id"].'">'.$rows["mcc"].'</td>
<td id="CPH_GridView1_mccmnc" style="width:250px" class=" mnc '.$rows["net_id"].'">'.$rows["mnc"].'</td>
<td id="CPH_GridView1_oldprice" style="width:320px" class=" oldprice '.$rows["net_id"].'">'.$rows["pricefrom"].'</td>
<td id="CPH_GridView1_newprice" style="width:129px" class=" newprice '.$rows["net_id"].'">'.$rows["priceto"].'</td>
<td id="CPH_GridView1_oldroute" style="width:143px" class="oldsupplierroute '.$rows["net_id"].'">'.$rows["routefrom"].'</td>
<td id="CPH_GridView1_newroute" style="width:143px" class=" newsupplierroute '.$rows["net_id"].'">'.$rows["routeto"].'</td>
<td id="CPH_GridView1_comments" style="width:143px" class=" comments '.$rows["net_id"].'">'.$rows["status"].'</td>
<td id="CPH_GridView1_from" style="width:143px" class=" fromdate '.$rows["net_id"].'">'.date('d.m.Y H:i', $rows["datetime"]).'</td>
<td style="width:65px" class=" '.$rows["net_id"].'"><input type="checkbox" name="chk1" value=" '.$rows["net_id"].'"/></td>
</tr>';
}
}
?>
</tbody>
jssearch.php
<?php
//connecting to db
$variable=$_POST['chk1'];
foreach ($variable as $variablename)
{
$sql_select="SELECT * from clientnetworkpricehistory where net_id=$variablename";
$queryRes = mysql_query($sql_select);
print"$sql_select";
}
echo "<table border='1'>
<tr>
<th>country</th>
<th>networkname </th>
<th>mcc</th>
<th>mnc</th>
<th>datetime </th>
</tr>";
while($row = mysql_fetch_array($queryRes))
{
echo "<tr>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['networkname'] . "</td>";
echo "<td>" . $row['mcc'] . "</td>";
echo "<td>" . $row['mnc'] . "</td>";
echo "<td>" . $row['datetime'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
my out put was(i am checked two checkboxes and i am trying to print two check boxes id it only showing one can any one help me how to do that)
56
Warning: Invalid argument supplied for foreach()
Use name="chk1[]" in your HTML so the checkboxes will be posted as an array.
Besides that:
The mysql_* functions are deprecated, consider using mysqli_* or PDO
You are vulnarable to SQL-injection on your where net_id=$variablename"
I agree with Peter but you also might wanna fix this little typo:
'.$rows['net_id'].'">'.$rows["country"].'
That's line 2 of that large echo you have with all the <td>s. The $rows['net_id'] should be double quotes.
But you knew that...:)

Categories