I'm working with an ODBC connection rather than MySQL. I have a search function which I have more or less copied over to apply with my ODBC connection, however they are not working. Here is my code except for the connection:
<!doctype html>
<html>
<title> Quoting System </title>
<head>
</head>
<body>
<form class="form" method="POST" action='try31.php'>
Quote Number <input class="form-control" type="text" name="quote" id="quote" placeholder="Enter Quote Number">
<br>   <input class="btn btn-default" type="submit" name="search" value="Search">
</form>
<table>
<tr>
<th>Company Name</th>
<th>Address1</th>
<th>Address2</th>
</tr>
<?php
if (!$conn){
if (phpversion() < '4.0'){
exit("Connection Failed: . $php_errormsg" );
}
else{
exit("Connection Failed:" . odbc_errormsg() );
}
}
if(isset($_POST['search'])){
$quote = $_POST['quote'];
$query = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
}
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
// Disconnect the database from the database handle.
//odbc_close($conn);
?>
</table>
</body>
</html>
As I don't get an error message, I know my connection is working, however currently, when I select the button, data does not appear as expected... Please help! Thanks
Found that with MS SQL ODBC connection the query syntax is different than MySQL. I changed where I was calling my columns in the table from:
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
To this:
$result = odbc_exec($conn, $stmt);
while (odbc_fetch_row($result)) // while there are rows
{
echo "<tr>";
echo "<td>" . odbc_result($result, "CompanyName") . "</td>";
echo "<td>" . odbc_result($result, "Address1") . "</td>";
echo "</tr>";
}
The odbc_result function was crucial here.
Related
I've been trying to display the image I uploaded into the db using a simple insertform. I've only been able to retrieve the image-name into the "data-file", what I'm wondering is how can I easily display the image in the same form location instead of the image-name?
This is my result at this moment, as you can see I've only added an image to one of the inserts, and I would like to change the "footer.jpg-name" with the image displayed here.
<html>
<head>
</head>
<body>
<form action="insertform.php" method="post">
Topic: <input type="text" name="topic"><br />
Name: <input type="text" name="name"><br />
Attendance: <input type="text" name="attendance"><br />
Image: <input type="file" name="image"><br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","username","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("testingisfun",$con);
$sql = "INSERT INTO Lectures (Topic,Name,Attendance,Image) VALUES ('$_POST[topic]', '$_POST[name]', '$_POST[attendance]', '$_POST[image]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
</body>
</html>
Heres the "data-file".
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("testingisfun",$con);
$sql = "SELECT * FROM lectures";
$myData = mysql_query($sql,$con);
echo "<table border = 1>
<tr>
<th>Topic</th>
<th>Name</th>
<th>Attendance</th>
<th>Image</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['Topic'] . "</td>";
echo "<td>" . $record['Name'] . "</td>";
echo "<td>" . $record['Attendance'] . "</td>";
echo "<td>" . $record['Image'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Thanks for any sort of feedback!
Try this
echo "<td><img src='/{$record['Image']}'>" . $record['Image'] . "</td>";
I took this answer from linked answer that I had put in comments. There are other alternatives also provided for this problem.
echo "<td>" . $record['Image'] . "</td>";
replace with
echo "<td><img src=\"data:image/jpeg;base64,"" . base64_encode( $record['Image'] ) . "/></td>";
You are storing images in DB and while fetching it is giving you image data that is raw so to display images from data you can use this method.
This wont help in caching but it will solve the problem.
So, whenever I click the add button and update the database, I have to refresh the page to see the results instead of the page auto refreshing after I click "update database".
Also, my delete button doesn't work at all. Please help D:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Movie Project</title>
</head>
<body>
<h1 style='text-align:center;'>Movie Collection Database</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method='post'>
<table align='center' border='0' cellpadding='3'>
<tr>
<?php
$fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Add");
echo "<tr>";
for($i=0;$i<7;$i++)
{
echo "<td>". $fieldsArray[$i]. "</td>";
}//end for
echo "</tr>";
?>
</tr>
<tr>
<td colspan='1' style='text-align:center'>
<input type="text" name="title" value="Movie Title">
</td>
<td>
<input type="text" name="studName" value="Studio Name">
</td>
<td>
<select name="movieRating">
<option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option>
<option value="Not Rated">Not Rated</option>
</select>
</td>
<td>
<input type="text" name="pubYear" value="2015">
</td>
<td>
<input type="number" name="IMDBRating" value="10.0">
</td>
<td>
<input type="number" name="time" value="0">
</td>
<td>
<input type="checkbox" name="addBox">
</td>
</tr>
</table>
<table align='center'>
<tr>
<td>
<input type="submit" name="submit" value="Update Database">
</td>
</tr>
</table>
</form>
<?php
echo "<table align = 'center', border = '1' >";
try
{
$db = new PDO("mysql:host=localhost;dbname=buckel", "buckel", "12345");
}
catch (PDOException $error)
{
die("Connection failed: ". $error->getMessage());
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $error)
{
$db->rollback();
echo "Transaction not completed: " . $error->getMessage();
}
include_once("Function.php");
if($_SERVER['REQUEST_METHOD']==="POST")
{
Delete($db);
$fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Delete" );
echo "<tr>";
for($i=0;$i<7;$i++)
{
echo "<td>". $fieldsArray[$i]. "</td>";
}//end for
echo "</tr>";
$query = "SELECT * FROM movie";
$statement = $db->prepare($query);
$statement->execute();
foreach(Prepare($db) as $row)
{
echo "<tr><td>" .$row['title'] . "</td>";
echo "<td>" .$row['studio'] . "</td>";
echo "<td>" .$row['rating'] . "</td>";
echo "<td>" .$row['pub_year'] . "</td>";
echo "<td>" .$row['imdb_rating'] . "</td>";
echo "<td>" .$row['run_time'] . "</td>";
echo "<td><input type='checkbox' name='". $row['id']. "' value='". $row['id']. "'>Delete</td></tr>";
}//end foreach
echo "</table>";
if(isset($_POST['addBox']))
{
$title=$_POST['title'];
$studio=$_POST['studName'];
$year=$_POST['pubYear'];
$movieRating=$_POST['movieRating'];
$IMDBRating=$_POST['IMDBRating'];
$time=$_POST['time'];
try{
$query2= $db->prepare("INSERT INTO movie (id, title, studio, rating, pub_year, imdb_rating, run_time) value (".'NULL'.", '$title','$studio','$movieRating','$year','$IMDBRating','$time')");
$query2->execute();
}//end try
catch(PDOException $error)
{
$db->rollback();
echo "There was an error";
$db=null;
die("Connection failed:" . $error->getMessage());
}//end catch
}//end if
if(isset($db))
$db=null;
}//end if for whole thing
?>
</body>
</html>
and here is the function file that i'm calling
<?php
function Prepare($db)
{
$q = $db->prepare("SELECT * FROM movie");
$q->execute();
return $q->fetchAll();
}//end Prepare(db object)
function Delete($db)
{
$q = $db->prepare("SELECT * FROM movie");
$q->execute();
foreach($q->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$q = $db->prepare("DELETE FROM movie WHERE movie.id=" . $row['id']);
$q->execute();
}//end if
}//end foreach
}//end Delete(db object)
You are using self page for printing showing etc.
If wanna to refresh page without reloading you have to use
else you can try like this
Javascript
window.location.reload(true);
// false - Default. Reloads the current page from the cache.
// true - Reloads the current page from the server
This will reload the page.
Php
header("Refresh:0");
I fixed the delete part not working by putting all of my php inside the form. I suppose that the php was not being submitted with the form
I would like to know how can i put the table from mysql in a new file.php.
I want the MySql table to be on the page.
This is my code that inserts data in MySql.
<?php
// Create connection
$con = mysqli_connect("host", "id_", "password", "xxxxxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$Task = $_POST['Task'];
$Date = $_POST['Date'];
$Desc = $_POST['Desc'];
$sql = "INSERT INTO tasklist (Task, Date, Description)
VALUES ('$Task', '$Date', '$Desc')";
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
<html>
<body>
<form action="addtask.php" method="post">
Task: <input type="text" name="Task">
Date: <input type="text" id="datepicker" name="Date">
Decrption:<textarea type="text" name="Desc"></textarea>
<input type="submit" value="submit">
</form>
</body>
</html>
Try this code:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
also u can try w3schools sample code :
Display the Result in an HTML Table
The following example selects the same data as the example above, but will display the data in an HTML table:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The output of the code above will be:
first code comes from "Jonnny" in this article
THIS IS MY CODE HERE I AM GETTING PROBLEM THIS IS A INPUT TYPE TAG AND NOT SELECTED DATA FROM DATABASE USING PHP.
here is first part of this code is html formatted ad second part is php
i want select data from database to data slect to same page as we see on the sopping cart sites
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpSelect</title>
</head>
<body>
Insert Age for search
<form action="#" method="post" >
<input type="text" id="val" name="resValue" />
<input type="submit" value="submit" /></form>
<?php
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
echo $res;
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Try changing the SQL to this
SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'
First issue I found on your code is here:
<input type="submit" value="submit" />
it should be:
<input type="submit" value="submit" name="submit" />
To be able to get the results. Below are the codes:
<?php
$query = "";
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
$query = " where Age='$res'"
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons $query");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change your select query to this one.
SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."'
In your problem the all value of form get by the name of all fields of form.\
so here should be
<input type="submit" name="submit" value="submit"/>
Because in $_POST['submit'] submit is same as button's name.
$result = mysqli_query($con,"SELECT * FROM Persons where Age="25");
I am trying to create a database and within the tables I have two options, insert and delete. I have the insert option working but can not get the delete function working for me. (I need to insert an update option once I have this working) I am including my code and am hopeful someone will see my error on this, I have been working on getting this project done for two weeks now. Thank You in advance.
<!DOCTYPE html>
<html>
<body>
<h1>Franchise Call Log</h1>
<?php
$con=mysqli_connect("localhost","tt2^homas12","c3o7P1518","tt2^homas12");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
<h1>Insert a New Caller</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Franchise: <input type="text" name="franchise">
<input type="submit" name="submit">
</form>
</body>
</html>
<html>
<body>
<h1>Delete a Caller</h1>
<form action="delete.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit" name="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Record has been updated</h1>
<?php
$con=mysqli_connect("localhost","tt2^homas12","c3o7P1518","tt2^homas12");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= ("DELETE FROM caller_info WHERE Lastname = '$Lastname'");
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record deleted";
mysqli_close($con);
?>
</body>
</html>
On delete.php you are not getting the data from $_POST
if(isset($_POST['lastname'])){
$Lastname=$_POST['lastname'];
}else{
echo "Error";die();
}
$sql= ("DELETE FROM caller_info WHERE Lastname = '$Lastname'");
Learn about prepared statements and use PDO or MySQLi with bind params - this article will help you decide which. If you choose PDO, here is a good tutorial.