Im pretty new on making webpages. But i´m doing a homepage with forms to Insert to my database. Thats no problem, my problem is that I want to show a specific column from the last row. And the code that I've got so far is this:
<html>
<body>
<form action="insert.php" method="post">
Publiceringsdag (OBS! En dag tidigare an foregaende):<br>
<?php
$con=mysqli_connect("localhost","rss","Habb0","kalender");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
or die(mysql_error());
echo $lastPub
?>
<br>
<input type="text" name="pub"><br>
<input type="submit">
</form>
</body>
</html>
Actually, it is not a very good idea to use the deprecated mysql_ functions. Look at PDO or Mysqli instead.
Meanwhile, in your current implementation you just need to fetch your data after the query execution:
$con = mysql_connect("localhost", "rss", "Habb0", "kalender");
if (mysql_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
or die(mysql_error());
if($row = mysql_fetch_assoc($lastPub)))
$result = $lastPub['pub'];
Now the result should be in your $result variable.
EDIT: I just noticed that in your code you use mysqli_connect, mysqli_connect_errno and mysql_query, mysql_error at the same time. But they belongs to different PHP extensions.
You must fetch the result first:
$lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
or die(mysql_error());
$result = mysql_fetch_array($lastPub);
echo $result['pub'];
Try this.
<html>
<body>
<form action="insert.php" method="post">
Publiceringsdag (OBS! En dag tidigare an foregaende):<br>
<?php
$con=mysql_connect("localhost","rss","Habb0") or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
$result = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1");
$data = mysql_fetch_array($result);
echo $data['pub'];
?>
<br>
<input type="text" name="pub"><br>
<input type="submit">
</form>
</body>
</html>
Related
here are two files,
LIST IMAGE:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("wordgraphic");
$sql = "SELECT id FROM userdata ORDER BY id DESC LIMIT 1,1";
$result=mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
$row = mysql_fetch_array($result);
echo $row['id'];
?>
<img src="imageView.php?image_id=<?php echo $row['id']; ?>" /><br/>
<?php
mysql_close($conn);
?>
</BODY>
</HTML>
And other file:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("wordgraphic") or die(mysql_error());
if(isset($_GET['id']))
{
$sql = "SELECT imageType,image FROM userdata WHERE id=" . $_GET['id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["image"];
}
else
"error";
mysql_close($conn);
?>
The second file is showing an image if i provide a static id but variable is not passed among the page and second issue is that when i echo :
$sql = "SELECT id FROM userdata ORDER BY id DESC LIMIT 1,1";
$result=mysql_query($sql);
It is giving id of not the last inserted query but 2nd Last inserted query.
Please help !
For the first problem change
if(isset($_GET['id']))
to:
if(isset($_GET['image_id']))
and for second problem
edit your query likr this :
SELECT id FROM userdata ORDER BY id DESC LIMIT 1
or
SELECT id FROM userdata ORDER BY id DESC LIMIT 0,1
SELECT id FROM userdata ORDER BY id DESC LIMIT 1
omit the coma and 1 on the last part of your query.
or if you want you can do this.
SELECT id FROM userdata ORDER BY id DESC LIMIT 0,1
My update script is not working.. I don't know what im missing..
but i can't update the table ... Went to w3school to know about the update in php but still it wont work...
-noob coder-
<?php
include 'Core/init.php';
protect_page();
include 'Includes/Overall/overallheader.php';
?>
<h1>Update School Year and Semester</h1>
<?php
$con=mysqli_connect("localhost","root","1234","database3");
// Check connection
$sy = $_POST['school_year'];
$sem = $_POST['semester'];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ( isset($_POST['submit'] ) )
{
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' AND `activesemester` = '$sem'";
$exec = mysql_query($sql) or die(mysql_error());
}
?>
<form action="" method="post">
<ul>
<li>
School Year:<br />
<input type="text" name="school_year">
</li>
<li>
Semester:<br />
<input type="text" name="semester">
</li>
<li>
<input type="submit" value="submit">
</li>
</ul>
</form>
<?php
include 'Includes/Overall/overallfooter.php';
?>
Even though its not directly realated:
Please make sure to escape Request data that you load from $_POST or $_GET by using mysql_escape_string . Otherwise it would be easy to inject SQL in your query, which would allow to run harmfull sql in your system, like deleting the database or manipulating the data.
http://en.wikipedia.org/wiki/SQL_injection
You have missed where condition in update query .. try this
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' AND `activesemester` = '$sem'" WHERE some_column=some_value;
First, you mix mysqli and mysql. Second, mysql_query/mysqli_query have two parameters. Third, your sql is not right.
So, change your $exec = mysql_query($sql) or die(mysql_error())
to $exec = mysqli_query($con,$sql) or die(mysqli_error($con));
change your sql to :
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' , `activesemester` = '$sem'";
but you didn't have a where condition here, if in your table you have set an
anto-increment key and set it to primary key, you could add some condition end of you sql,otherwise it will
update all your records.
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' , `activesemester` = '$sem' where id={$id}"
$id is one recod from you table.
this should work for you excute an update..
Try using this :
1) use "action" attribute of the tag
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
2) check whether your form has beensubmitted :
if(isset($_POST['submit']))
3) Use WHERE condition in your sql query. You final query should look like this :
<?php
if(isset($_POST['submit']))
{
$con = mysqli_connect("localhost","root","1234","database3");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$query = "
UPDATE `activesys`
SET `activeschoolyear` = '$sy',`activesemester` = '$sem'
WHERE `columnName` = `columnValue`
";
mysqli_query($con,$query);
}
This is the error message I am getting:
Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/50/8492150/html/colejoh/web/ai/form.php on line 10
For my HTML form I am using this code:
<html>
<head>
</head>
<body>
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<body OnLoad="document.myform.query.focus();">
</body>
</html>
And for my form.php code:
<body>
<?php
$con = mysql_connect ("xxx.xxx.xxx.xxx", "user", "password");
mysql_select_db ("user", $con);
if (!$con)
{
die ("Could not connect: " . mysql_error());
}
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE $_POST["term"];") or die
(mysql_error());
while ($row = mysql_fetch_array($sql)){
echo 'ID: ' .$row['id'];
echo '<br /> Key: ' .$row['key'];
echo '<br /> Page: '.$row['page'];
}
mysql_close($con)
?>
</body>
I believe the error is in the form.php in the sql statement at LIKE $_POST["term"];") What I am trying to do is to make that code to be what I submitted on the form page.
This line is causing the problem:
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE $_POST["term"];") or die
Change it to:
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE " . mysql_real_escape_string($_POST["term"]) . ";") or die
Also you should avoid using the mysql_ functions they are deprecated. Rather you should use mysqli_ or PDO. This code is also vulnerable to sql injection.
You are not encoding the double quotes correctly. Change the $sql assignment to
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE '".mysql_real_escapse_string($_POST["term"])."'") or die(mysql_error());
This will also guard you from sql injections.
try this
$sql = mysql_query("SELECT * FROM ai
WHERE key LIKE '".mysql_real_escape_string($_POST['term'])."'")
or die(mysql_error());
In php you have to be very careful when working on string variables. To connect multiple string variables you should do it like this:
"SELECT * FROM ai WHERE key LIKE" . $_POST["term"] . ";"
To make your life easier, you should often create "helper" variables like this:
$my_variable="SELECT * FROM ai WHERE key LIKE" . $_POST["term"] . ";"
And then add it to your script like so:
$sql = mysql_query($my_variable) or die(mysql_error());
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE 'mysql_real_escapse_string($_POST[term])'");
Update the above query.
There should not be any double quotes for term
First off, you should not post your mysql username and password.
Second of all, The search term should be quoted.
Replace your line error by this one :
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE $_POST[\"term\"];") or die
(mysql_error());
try this
<body>
<?php
$con = mysql_connect ("xxx.xxx.xxx.xxx", "user", "password");
mysql_select_db ("user", $con);
if (!$con)
{
die ("Could not connect: " . mysql_error());
}
$sql = mysql_query("SELECT * FROM ai WHERE key LIKE ".$_POST["term"].";") or die
(mysql_error());
while ($row = mysql_fetch_array($sql)){
echo 'ID: ' .$row['id'];
echo '<br /> Key: ' .$row['key'];
echo '<br /> Page: '.$row['page'];
}
mysql_close($con)
?>
</body>
I am coding some PHP working with two MySQL databases. What I am working toward is different information to be sourced from the two databases which will then populate some form fields like the drop-down menu. The form will then be posted to create a printable document yada yada...
What Works
The connection to the first database works fine, the field is populated and there are no errors.
What Doesn't Work
When I introduce the second Database I get no errors but the form wont populate. I make this change...
From One Database:
$sql = mysql_query"SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";
To Two Databases:
$sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
The Connection
Source: http://rosstanner.co.uk/2012/01/php-tutorial-connect-multiple-databases-php-mysql/
How do you connect to multiple MySQL databases on a single webpage?
<?php
// connect to the database server
$conn = mysql_connect("localhost", "cars", "password");
// select the database to connect to
mysql_select_db("manufacturer", $conn);
// connect to the second database server
$conn2 = mysql_connect("localhost", "cars", "password");
// select the database to connect to
mysql_select_db("intranet", $conn2);
?>
The Execution
It appears that $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn); Is my problem
<form name="form" method="post" action="review.php">
<table><td>
<select>
<option value="">--Select--</option>
<?php $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
$rs_result = mysql_query ($sql);
// get the entry from the result
while ($row = mysql_fetch_assoc($rs_result)) {
// Print out the contents of each row into a table
echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
}
?>
</select>
</td></table>
</form>
Thanks In advance for any help :)
you've got 2 mysql query commands going...
<?php
$sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
$rs_result = mysql_query ($sql); // <-- $sql here is the result of the first query (ie. not a sql command)
should be
<form name="form" method="post" action="review.php">
<table><td>
<select>
<option value="">--Select--</option>
<?php
$sql = "SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";
$rs_result = mysql_query( $sql, $conn );
// get the entry from the result
while ($row = mysql_fetch_assoc($rs_result)) {
// Print out the contents of each row into a table
echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
}
?>
</select>
</td></table>
</form>
good luck!
<html>
<form action="update.php" method="POST" name="ID">
<input type="text" name="ID"">
<input type="Submit" value="Submit">
</form>
</html>
Up there is the submit form to get an ID number.
I try to get that ID entered by user ( NOTE: It's a number) and show mysql table row coresponding to that ID.
Example : User enter 2 and row number 2 from database is shown.
My problem is that all rows are shown and not only wanted one.
- Extra Question : How can I show user an error if he entered a NULL value ?
<?php
$id=$_POST['ID'];
.
.
.
mysql_connect($host,$username,$password);
if (!mysql_select_db($database))
die("Can't select database");
$query="SELECT * FROM table WHERE ID= '$id'";
$result = mysql_query("SELECT * FROM vbots");
$num=mysql_num_rows($result) or die("Error: ". mysql_error(). " with query ". $query);
mysql_close();
.
.
.
?>
You're not running your query.
You have this:
$query="SELECT FROM table WHERE ID= '$id'";
$result = mysql_query("SELECT * FROM vbots");
You want this:
$query="SELECT FROM table WHERE ID= '$id'";
$result = mysql_query( $query);
**Insert nag about SQL injection**
It should be this.
<?php
$id=$_POST['ID'];
mysql_connect($host,$username,$password);
if (!mysql_select_db($database))
die("Can't select database");
$query="SELECT * FROM table WHERE ID= '$id'";
$result = mysql_query($query);
$num=mysql_num_rows($result) or die("Error: ". mysql_error(). " with query ". $query);
mysql_close();
?>
to check if $id is null:
if (!isset($id))
die("Enter a value for id!");