<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!");
Related
I am trying to get the last Employee ID from my Sqli table, increment the id retrieved from the table and insert the new value into the table along with the new record.
The code is not working as the table is not getting updated.
<form method="POST">
<input type="text" name="brn" placeholder="Branch"/>
<input type="text" name="nam" placeholder="Enter Name"/>
<input type="submit" name="insert">
<?php
$db=mysqli_connect("localhost","root","","test");
session_start();
if(isset($_POST['insert']))
{
$brn = $_POST['brn'];
$nam = $_POST['nam'];
$qry = "SELECT * FROM emp";
$result=mysqli_query($db,$qry);
$row = mysqli_fetch_array($result);
$empid= $row["empid"];
$empid++;
$query = "INSERT INTO `emp`(`brn`,`nam`,'empid') VALUES ('$brn','$nam','$empid')";
mysqli_query($db,$query);
mysqli_close($db);
}
?>
</form>
You need some changes in your code.
You are selecting the first "EmpId" instead of the last one and if you are having the column empid as primary key, it will show the error of primary key violation.
Therefore change your query to somewhat this:
$qry = "SELECT * FROM emp order by empid desc limit 1";
It will return one row.
And, if you just need the last empid then i would suggest you to go with only:
$qry = "SELECT empid FROM emp order by empid limit 1";
This is more resource effective query.
****Happy Coding.****
you can try this. I hope it will help you.
$sql = "INSERT INTO `emp`(`brn`,`nam`,'empid') VALUES ('$brn','$nam','$empid')";
if(mysqli_query($db, $sql)){
$last_id = mysqli_insert_id($db);
echo "Records inserted successfully. Last inserted ID is: " . $last_id;
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>
Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}
You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);
You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}
I'm working on trying to input data from mysql into one field, below is mysql code: what I'm trying to do is have all the data from $row['db_numbers']; load into the
<input type="text" name="db_numbers" values="<?php echo $row['db_numbers'];?>
but it makes double the fields
were i end up with multiple db_number fields, I only want one.
<?php
$query = "SELECT * FROM sms_numbers WHERE `db_user` ='".$user_name."'";
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$group_sms = $row['db_numbers'];
}
?>
Try this:
<?php
$query = "SELECT * FROM sms_numbers WHERE `db_user` ='".$user_name."'";
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
WHILE($row = mysql_fetch_assoc($result)) {
$group_sms .= $row['db_numbers'];
}
?>
And then:
<input type="text" name="db_numbers" values="<?php echo $group_sms;?>">
Notice the '.=' in the while loop. It concatenates the numbers. Maybe add some spaces between them if you want ('. " "')?
WHILE($row = mysql_fetch_assoc($result)) {
$group_sms .= $row['db_numbers'] . " ";
}
Then echo the $group_sms variable since that contains all the db_numbers.
Hope that helps.
I think, if you are directly returning $row['db_numbers'] after querying the DB, then you can just print the $group_sms in the <input /> tag.
So my opinion would be:
- either return $row after querying the DB
- or directly use $group_sms in <input /> tag
If you're just trying to create an input element containing the value from a single field in the database there is no need for looping, use this:
<?php
$sql = "SELECT db_numbers FROM sms_numbers WHERE username = '$user_name'";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$db_numbers = $value->id;
?>
If you have multiple values that you want to concatenate or sum you could do that directly in the query and use the same code otherwise:
$sql = "SELECT GROUP_CONCAT(db_numbers SEPARATOR '') AS db_numbers FROM sms_numbers WHERE username = '$user_name'";
$sql = "SELECT SUM(db_numbers) AS db_numbers FROM sms_numbers WHERE username = '$user_name'";
Then use this to output:
<input type="text" name="db_numbers" values="<?php echo $db_numbers;?>
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);
}
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>