Retrieve values from a database and importing into table rows - php

Good day guys, I will be glad if you can help me out.
I have a project. and the remaining part of it is where i can pull out data from the database.
Its a tracking application. I want a situation whereby if a user type his/her reference number, it will display the results in a tabular from i.e
Reference Number, Ship date, ship weight, excpected delivery, ship country, ship state, city, zip, e.t.c. I have the following html code:
<form id="track" name="track" method="post" action="tester.php">
<h2>Track your shipment Here</h2>
<p><label> Tracking Reference:
<input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>
<div class="button_holder">
<p> <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
</label></p>
</div>
and my php code is:
<?php
$ref = mysql_real_escape_string($_POST['reference']);
// conmnecting to the database
if(isset($ref)){
$connection = mysql_connect('localhost', 'root', 'wisdom');
mysql_select_db('gday');
$query = "SELECT * FROM shipment WHERE id = '".$ref."' ";
$row = mysql_query($query);
echo "<table border=\"1\"; width='550px'>"; // start a table tag in the HTML
//Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['ship_type'] . "</td><td>" . $row['ship_ref'] . "</td><td>" . $row['ship_date'] . "</td><td>" . $row['ship_weight'] . "</td><td>" . $row['expected_delivery'] . "</td></tr>" . $row['ship_country'] . "</td></tr>" . $row['ship_state'] . "</td></tr>" . $row['ship_city'] . "</td></tr>" . $row['ship_zip'] . "</td></tr>" . $row['remark'] . "</td></tr>";
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
}
else{echo " Enter a valid Reference Number";
exit;}
?>
Now, my major problem is this: the following code output just the table without any information inserted, Please what could be done to make sure the rows are filled with data retrieved from the database?
Thanks

Fetch fetch fetch.......
Change it like this:
$res= mysql_query($query);
$row=mysql_fetch_assoc($res);
mysqli_ functions are deprecated, don't use them anymore. Use the MySQLi or PDO libraries instead.

Related

Problems viewing/editing a date in php/mysql

I have a php page that is creating a table from a mysql database. In that table I have an edit button to edit the data and update the mysql database. All is working great except one column that is a date. If I pull the date into a text input it works fine, but I'd prefer this to be a date field ideally to have the calender selector.
When I change the input type to date it doesn't pull the date over - instead it displays dd / mm / yyyy. This is annoying as if I only need to change the other fields and not the date I have to manually add the date again each time I try to update. Could anyone advise how to populate the date box with the date from the mySQL database?
My code (where shipping_date is the affected date):
index.php displaying the table
$sql = "SELECT * FROM final_view1 WHERE freight = 'Fedex' OR freight = 'DHL' OR freight = 'IBC' ORDER BY shipping_date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["shipping_date"] . "</td><td>" . $row["freight"]. "</td><td>" . $row["tracking_no"] . "</td><td>" . $row["vendor_name"] . "</td><td>" . $row["receiver"] . "</td><td>" . $row["pieces"] . "</td>
<td>" . $row["days_in_transit"] . "</td><td>" . $row["courier_status"] . "</td><td>" . $row["comments"] . "</td><td align=right><a href='courier_edit.php?id=".$row['id']."' class='button-edit1'>Edit</a>&nbsp
<a href='complete.php?id=".$row['id']."' class='button-complete'>Complete</a></td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
courier_edit.php
<?php
include('db.php');
$result = $conn->prepare("SELECT * FROM final_view1 where id='$ID'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$id=$row['id'];
?>
<form class="form-horizontal" method="post" action="courier_edit_db.php<?php echo '?id='.$id; ?>" enctype="multipart/form-data";">
<table class="styled-table">
<tr class="active-row">
<td><label for="inputPassword">Company</label></td>
<td><input type="text" name="freight" required value="<?php echo $row['freight']; ?>"></td>
<td><label for="inputPassword">Ship date</label></td>
<td> <input type="date" name="shipping_date" required value="<?php echo $row['shipping_date']; ?>"></td>
You said - "If I pull the date into a text input it works fine"
What is the date format written there ?
to fit your date into your <input type="date"> you need to change the date format,
You need to change the date format to standard date format.
I hope you understand what I want to say , try something similar to this -
<input name="any_name" type="date"
value="<?php echo date('Y-m-d',strtotime($data["congestart"])) ?>"/>

Form entry table search results in opening a new page and error

Shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table.
I am not really sure what I am doing wrong, I just started php and have been using w3 to navigate the syntax. I managed to make a plugin that would display the a entire table but now I am trying to use a form to search a table and display the results.
...
if( isset($_GET['submit']) ){
$ssterm = $_POST["name"];
$query = "SELECT ID, Retailer, City, State, Contact, Address1, Address2, Country, Zip, Email, Website FROM Stores WHERE Retailer LIKE '%" . $rssterm . "%' OR City LIKE '%" . $ssterm ."%'";
$result = $conn->query($query);
if($result->num_rows > 0){
echo "<table>
<tr>
<th>ID</th>
<th>Retailer</th>
<th>City</th>
<th>State</th>
<th>Contact</th>
<th>Address1</th>
<th>Address2</th>
<th>Country</th>
<th>Zip</th>
<th>Email</th>
<th>Website</th>
</tr>";
while($row = $result->fetch_assoc()){ //Creates a loop to loop through results
echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['Retailer'] . "</td><td>" . $row['City'] . "</td><td>" . $row['State'] . "</td><td>" .
$row['Contact'] . "</td><td>" . $row['Address1'] . "</td><td>" . $row['Address2'] . "</td><td>" . $row['Country'] . "</td><td>" . $row['Zip'] .
"</td><td>" . $row['Email'] . "</td><td>" . $row['Website'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
}
else {
echo "No Results Found...";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="search">
</form>
<?php
$conn->close(); //Make sure to close out the database connection
} // end of a shortcode brace
?>
I thought this would for sure work, but it just shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table. I am using this on a wordpress site and as a plugin using a shortcode to implement it into a page.

PHP: include variable in $_POST

I have a table with some information from my database. I have buttons next to each row in the table. If the button is clicked it should send an email to the user. I can give each button a unique value but can not have this value in the if isset.... How should i get the value of the button name to be variable and how could i get the value at the end of my url.
echo "<table>";
echo "<tr><td>Naam</td><td>EmailAdress</td><td>Datum</td><td>Type</td><td>Producent</td><td>SerieNummer</td><td> imei</td><td>Manager</td><td>bevestigd</td></tr>";
while($row = mysqli_fetch_row($result)){
$sqlid = "SELECT Bevestigd FROM tbl_bevestiging WHERE IDbewijs = " . $row[0];
$resultid = mysqli_query($conn, $sqlid);
$row2 = mysqli_fetch_row($resultid);
echo "<tr><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td><td>" . $row [6] . "</td><td>" . $row[7] . "</td><td>" . $row[8] . "</td><td>" . $row2 [0] . "</td><td><form method=\"post\"><input name=\"" . $row[0] . "\" type=\"submit\" value=\"" . $row[0] . "\"</form></td></tr>";
}
echo "</table>";
if (isset($_POST[$row[0]])){
$admin_email = "adminmail#mail.com," . $row[2];
mail($admin_email, "Ontvangstbewijs geleverde hardware herverstuurd", "url?id=" . $row [0] , "From:" . "example#email.com");
mysqli_close($conn);
}
?>
Theres a few things I think are worth pointing out firstly.
Table header tags should be in a THEAD tag. See example here.
Try and avoid SQLs within a loop. If possible create a SQL that does everything for you to reduce the number of queries sent to MySQL.
Ok back to your question. Personally, what I would do is have a FORM tag in a TD tag. This way each button can have it's own unique id set in a hidden INPUT tag.
For example:
<tr>
<td>
<form action="[[YOUR-PHP-FILE]]" method="post">
<input type="hidden" name="id" value="[[ID]]" />
<input type="submit" value="Submit" />
</form>
</td>
</tr>
That way you don't need to focus on the submit button but rather the id.

PHP not fetching data from MySQL database

I'm making a comment box, in which
Comment box will be under the post
Every comment box has the same id as the post has
That's why i preffered to do a while loop
But, the problem is
As i'm using a fetch_assoc() method, some comment tables will be empty so
i've made a if else code for them
Here is my if else code :
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
$result_query_select_comment_table = $dbc->query($select_comment_table);
if(!$result_query_select_comment_table) {
$result_select_comment_table = array("full_name" => "", "comment"=> "No comments yet.");
}
else {
$result_select_comment_table = $result_query_select_comment_table->fetch_assoc();
}
If other code is need :
echo '<div id="feed_comment_box_' . $row['id'] . '"' . 'class="feed_comment_box_cl"><div id="add_comment_id" class="add_comment_cl">
<form class="comment_form" method="post" action="' .$_SERVER['PHP_SELF'] . '">
<input name="comment_full_name" type="text" class="input_comment_full_name"> </input>
<textarea name="input_comment_text" type="text" class="input_comment_text" ></textarea><input name="comment_submit" type="submit"></input> <br>
</form>
</div><br>
<div id="comment_box_id" class="comment_box_cl">
<table tabindex="0" class="comment_box">
<tr> <td class="comment_full_name">' . $result_select_comment_table["full_name"] . '</td></tr><br>' .
'<tr> <td class="comment_full_name">' . $result_select_comment_table["comment"] . '</td></tr><br>'
. '</table></div></div>';
echo '</div>';
Problem : As you see the if else code, the php should echo no comments yet when there is no comment. But, even after inserting the comment(i tried manually insert in database) it is showing no comments yet whereas it should show the comments
Note: I am using a while loop that's why the comment div is coming under every post.
It looks like you're missing a space in your query:
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
should be
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . " ORDER BY id";
The invalid query is causing $result_query_select_comment_table to always be false.

Need assistance displaying current mysql record and modifying in php form

Thanks in advance for any light shed.
I have a mysql database consisting of customers with some fields pertaining to each customer. currently running on one of my lamp servers. There is security risks with my code at the moment, but I plan to get the functionality i'm looking for and then reconfigure the code for a tighter security. At the moment I have an html index file that calls on php script to search mysql database by firstname or lastname. Upon this query it displays a list of users and allows me to modify the user. When I click modify it pulls the correct customer id number, but it is not displaying any current information, nor allowing me to update the info.
To summarize, I would like to search a customer, and it pull up selected fields and show the content and allow me to actively change the data and resend it to the database.
My search.html code:
<html>
<body>
<form action="scripts/search.php" method="post">
Firstname: <input type="text" name="firstname">
<input type="submit">
</form>
<form action="scripts/lastnamesearch.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>
<form action="scripts/phonenumbersearch.php" method="post">
Phone Number: <input type="text" name="phone">
<input type="submit">
</form>
</body>
</html>
MY search.PHP Script:
//this script allows me to search the database by filling out one of the forms and clicking submit. Each of the forms calls upon it's own individual script, I realize that this is probably cumbersome, due to my lack of coding knowledge.
<?php
$con=mysqli_connect("localhost","root","*****","*******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers WHERE `firstname` LIKE '$_POST[firstname]'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>phone</th>
<th>address</th>
<th>notes</th>
<th>additional notes</th>
<th>passwords</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['addnotes'] . "</td>";
echo "<td>" . $row['passwords'] . "</td>";
echo "Modify User";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My modify.php script:
//this is where I believe one of my problems lie. when I click modify user on the search.php script it calls on this script and it loads the correct user/customer id in the address bar, but it doesn't show any existing data, nor does it update the data that I fill in the cells.
<?php
$con=mysqli_connect("localhost","root","crapola1","Computition");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mysqli_query = "SELECT * FROM customers WHERE ID = $_get[id]";
$mysqli_result = mysqli_query($mysqli_query);
$customer = mysqli_fetch_array($mysqli_result);
?>
<h1> You are modifying a user</h1>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Firstname<input type="text" name="inputFirstname" value="<?php echo $row['firstname']; ?>" /><br />
Notes<input type="text" name="inputNotes" value="<?php echo $row['notes']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify" />
</form>
Thanks again,
I've been searching on this topic for about a week now and have pieced together this much, but can't seem to get over this "hump"
$_GET is a super global array . It should be in UPPERCASE.
Change the query on your modify.php here
SELECT * FROM customers WHERE ID = $_get[id] to upper case.
Must be..
SELECT * FROM customers WHERE ID = ".$_GET['id']
Also, It is strictly not advised to pass the $_GET or $_POST parameters directly to your query as it leads to SQL injection. You need to switch over to PreparedStatements

Categories