This is my code :
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="blah"; // Mysql password
$db_name="test"; // Database name
$tbl_name="SubCategories"; // Table name
$con=mysqli_connect("$host", "$username", "$password", "$db_name");
if (mysqli_connect_errno()) // Check connection
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="untitled.php" method="post"><!-- untitled.php -->
<?php
//print_r($_POST); //print all checked elements
//echo "<br>".$email, $_POST["update"][$i];
//mysql_real_escape_string ($route )
if(isset($_POST['submit'])) {
foreach ($_POST["holder"] as $i=>$email) {
$y=$email;
$h=$_POST["update"][$i];
$res2=mysqli_query("UPDATE ".$tbl_name." SET subCat2 = '" . $y . "' WHERE id =". $h,$con);
if ($res2){
}
else{
echo "<h1>NOT WORKING!</h1>";
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
}
$result = mysqli_query($con,"SELECT * FROM $tbl_name");
echo "<br>";
while($row = mysqli_fetch_array($result))
{
echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['subCat2'] . '"/>';
echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['subCatNum'] . '"/>';
echo "<br>";
}
?>
</br>
<input type="submit" name="submit">
</form>
</body>
</html>
I can't update the table in my database. I am able to extract the variables properly and echo them, however it does not work.
I have gotten the following error in the past 'no database selected'.
I think that you forgot to select the database. Try to put this after your connection:
if (!mysqli_select_db($con, $db_name)) {
die("Uh oh, couldn't select database $db_name");
}
If this happens, double check the name, permissions, etc.
Try it again, but without the quotes surrounding the DB connection variables. I mean, they are variables & not strings, right?
Original with quotes:
$con=mysqli_connect("$host","$username","$password","$db_name");
Cleaned without quotes:
$con=mysqli_connect($host,$username,$password,$db_name);
You should change your code adding the snippet below. This way you can debug your code better:
if (!$result = $mysqli->query("YOUR-SQL", MYSQLI_USE_RESULT)) {
printf("Error: %s\n", $mysqli->error);
}
...do something here..
$result->close();
Someone in my class helped me figure it out, thanks though! Here is the code, just wonderful :)
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="blah"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$con=mysqli_connect($host,$username,$password,$db_name);
if (mysqli_connect_errno()) // Check connection
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="untitled.php" method="post"><!-- untitled.php -->
<?php
if(isset($_POST['submit'])) {
foreach ($_POST["holder"] as $i=>$email) {
$y=$email;
$h=$_POST["update"][$i];
$sql2="UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h;
//$res2=mysqli_query("UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h,$con);
$res2=mysqli_query($con,$sql2);
if ($res2){
}
else{
echo "<h1>NOPE!</h1>";
print "Failed to connect to MySQL: " . mysqli_error();
}
}
}
$result = mysqli_query($con,"SELECT * FROM ".$tbl_name);
echo "<br>";
while($row = mysqli_fetch_array($result))
{
echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['name'] . '"/>';
echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['id'] . '"/>';
//echo '<input type="text" class="a" name="holder2[]" id="checkbox-1" class="custom" value="' . $row['price'] . '" />';
echo "<br>";
}
?>
</br>
<input type="submit" name="submit">
</form>
</body>
</html>
Related
Im just starting out with MySQL and PHP. Im trying to create a drop-down menu, to pick a certain competence within a company.
Later on, i want a total of 3 dropdowns, so that i can combine user/competence/avaliable date, to display sort of a calendar to show which users are avaliable a certain date, with information about their competence.
However, the code below just returns this:
Result
The same query to the database returns 12 values.
What am i doing wrong? I get no errors, just a blank drop-down.
<!DOCTYPE html>
<html>
<head>
<title> Greetings. </title>
<meta charset="UTF-8"/>
</head>
<body>
<?php
session_start();
$con = mysqli_connect("127.0.0.1", "root", "", "service");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
echo "<br />";
$query_kompetens = "SELECT kompetens FROM kompetens";
$kompetens = mysqli_query($con, $query_kompetens);
echo "<select name='Kompetens'>";
echo "<option size =30 ></option>";
while ($row = mysqli_fetch_array($kompetens)) {
echo "<option value='" . $row['Kompetens'] . "'>" . $row['Kompetens'] . "</option>";
}
echo "</select>";
;
?>
</body>
</html>
Try This code, actually you used $row['Kompetens'] instead of $row['kompetens'].
<!DOCTYPE html>
<html>
<head>
<title> Greetings. </title>
<meta charset="UTF-8"/>
</head>
<body>
<?php
session_start();
$con = mysqli_connect("127.0.0.1", "root", "", "servicedesk");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
echo "<br />";
$query_kompetens = "SELECT kompetens FROM kompetens";
$kompetens = mysqli_query($con, $query_kompetens);
echo "<select name='Kompetens'>";
echo "<option size =30 ></option>";
while ($row = mysqli_fetch_array($kompetens)) {
echo "<option value='" . $row['kompetens'] . "'>" . $row['kompetens'] . "</option>";
}
echo "</select>";
;
?>
</body>
</html>
So, I am trying to figure out how do this this and it boggling me. THIS WILL NOT BE USED ONLINE LIVE SO SQL INJECTION I DONT' CARE ABOUT. What am I doing wrong/right?
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$comment = $_GET['comment'];
$id = $_GET['id'];
$sql = "UPDATE Dbsaved SET comment = '{$comment}' WHERE id = $id";
$comment1 = mysql_query($sql);
if (!$comment1) {
die("did not save comment: " . mysql_error());
}
echo $sql;
The main problem is with the statement itself, the connection is fine. I am trying to read $comment, and then update that into a MYSQL table and then have it read back in a different file.
EDIT: Mark up for the form I'm taking $comment from.
<!DOCTYPE html>
<html lang="en">
<LINK href="stylesheet.css" rel="stylesheet" type="text/css">
<script src ="js/validateform.js"></script>
<head>
<meta charset="UTF-8">
<title>UniHelp Home</title>
</head>
<body>
<div id="headeruni">
<h1>Welcome <?php echo $_GET["name"]; ?> to UniHelp!</h1>
</div>
<div id ="infouni">
<h3>Welcome to UniHelp. The social Network getting you connected to other people all over the University for any help you require!</h3>
</div>
<div id ="nameandemail">
<form action="formsend.php" method="post">
First name: <br> <input type="text" name="name"><br>
Email: <br> <input type="text" name="email"><br>
Comment: <br> <input type="text" name="message"><br>
<input type="submit" name="submit">
</form>`enter code here`
</div>
<div id="grabphpdiv">
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$result = mysql_query("SELECT * FROM Dbsaved", $db);
if (!$result) {
die ("Database query failed: " . mysql_error());
}
$comment = $_POST['$comment'];
while ($row = mysql_fetch_array($result)) {
echo "<div id='posts'>";;
echo "<h2>";
echo $row[1] . "";
echo "</h2>";
echo "<p>";
//echo $timestamp = date('d-m-y G:i:s ');
echo "<br>";
echo "<br>";
echo $row[2] . "";
echo "</p>";
echo "<p>";
echo $row[3] . "";
echo "</p>";
echo 'Delete';
echo "<br>";
echo "<br>";
echo 'Comment: <br>
<input type=text name=comment><br>
<a href=addcomment.php?id=' . $row[0]. '&comment='. $row['$comment'].'>Comment</a>';
echo "<p>";
echo $row['comment'] . "";
echo "</p>";
echo "</div>";
echo "<br>";
}
?>
</div>
</body>
<div id="footer">Copyright © James Taylor 2016</div>
</html>
I just ran this code:
$comment = "Hello World!";
$id = 1;
$sql = "UPDATE Dbsaved SET comment = '{$comment}' WHERE id = {$id}";
echo $sql;
and saw:
UPDATE Dbsaved SET comment = 'Hello World!' WHERE id = 1
which is a correct SQL statement, so if it is not working, you might want to play with SQL directly to get something working. Hope that helps!
SOLUTION:
$comment = $_GET['$comment'];
$id = $_GET['$id'];
while ($row = mysql_fetch_array($result)) {
echo "<div id='posts'>";;
echo "<h2>";
echo $row[1] . "";
echo "</h2>";
echo "<p>";
//echo $timestamp = date('d-m-y G:i:s ');
echo "<br>";
echo "<br>";
echo $row[2] . "";
echo "</p>";
echo "<p>";
echo $row[3] . "";
echo "</p>";
echo 'Delete';
echo "<br>";
echo "<br>";
echo $row[4] . "";
echo "<br>";
echo 'Comment: <br>
<form action="addcomment.php?id=' . $row[0]. '" method="post">
<input type=text name=comment><br>
<input type=submit name="submit">
</form>';
echo "<p>";
echo $row['comment'] . "";
echo "</p>";
echo "</div>";
echo "<br>";
}
?>
and:
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$comment = $_POST['comment'];
$id = $_GET['id'];
$sql = "UPDATE Dbsaved SET comment = '$comment' WHERE id = $id ";
$comment1 = mysql_query($sql);
echo $sql;
if (!$comment1) {
die("did not save comment: " . mysql_error());
}
else {
header("location: UniHelpindex.php");
}
It was to do with mainly needing to get the id which was used in $row[0]' in the form created in the while loop. And actually using the correct syntax for the update Dbsaved... bit.
I have started to make a program in PHP and it's mostly finished.
But, now I don't know how to open a link on the browser grabbed from database row.
Can someone help me please?
<center><br><br><form>
<input type="button" value="link10" onclick="window.location.href='echo '<center>'.$rows['root_url10']'" />
</form></center>
Is that possible something like that?
<?php
$location = "";
$db = mysqli('localhost', 'root', '', 'databasename');
$sql = "SELECT link FROM tablename";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
$location = $row['link'];
}
if(isset($_POST['newlink'])) {
header('Location: ' . $location);
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="newlink" value="open page">
</form>
</body>
</html>
NOTE: you can also make the header function a redirect function all the header function does is redirect see more here: http://www.w3schools.com/php/func_http_header.asp
the $_server['php_self'] just gets the url it is like if it was stackoverflow.com/try.php it will get that URL
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username="publiadd_publix"; // Mysql username
$password="1a3g7893fsh"; // Mysql password
$db_name="publiadd_urlrotator"; // Database name
$tbl_name="url_rotator"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM url_rotator ORDER BY root_name DESC LIMIT 10";
$result=mysql_query($sql);
// Print Title
print '<center>'."All Rotator In Our Network! ".'</center>';
print '<center>'."---------------------------".'</center>';
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
echo '<center>'.$rows['id'] . " | " . $rows['root_name'] . " | " .'</center>' ;
echo '<center>'.$rows['root_url1'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url2'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url3'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url4'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url5'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url6'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url7'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url8'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url9'] . " * " .'</center>' ;
echo '<center>'.$rows['root_url10'] . " * " .'</center>' ;
// close while loop
}
// close MySQL connection
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body bgcolor="#ffffff">
<center><br><br><form>
<input type="button" value="Back" onclick="window.location.href='http://bitcoinrotator.publiadds.org.pt/login-registration/home.php'" />
</form></center>
</body>
</html>
I am building a simple webpage that will allow the user to delete a record from a database and then the page will reload with the record deleted. Having trouble figuring out the code to use to accomplish this. Here is my main page:
<html>
<head>
<title>Change Record form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: .875em }
</style>
</head>
<body>
Add new record
<br>
<br>
<?php
$con=mysqli_connect("localhost", "root", "", "customers");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$strSQL = "SELECT * FROM music";
$rs = mysqli_query($con, $strSQL);
while($row = mysqli_fetch_array($rs, MYSQLI_BOTH)) {
// Write the value of the column FirstName (which is now in the array $row)
echo "<br>";
echo "<br>";
echo $row['artist'] . "<br />";
echo $row['title'] . "<br />";
echo $row['format'] . "<br />";
echo $row['notes'] . "<br />";
echo '<FORM METHOD="LINK" ACTION="update_form.php">
<INPUT TYPE="submit" VALUE="Update">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="delete_process.php">
<INPUT TYPE="submit" VALUE="Delete">
</FORM>';
}
?>
And then this is the delete_process page:
<?php
$id = $_GET['id'];
$artist = $_GET['artist'];
$title = $_GET['title'];
$format = $_GET['format'];
$notes = $_GET['notes'];
//create connection to DB
$con=mysqli_connect("localhost", "root", "", "customers");
//check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "DELETE from 'MUSIC' WHERE 'id' = $id";
mysql_query($sql);
?>
Basically when the user clicks the delete button, that particular record will be deleted and the main page will reload with the record deleted. Don't really know the code, thanks!
add a header at the end of your delete_process.php
<?php
//redirect to index page
header('Location:index.php'); //replace index.php with the page you want to redirect to
?>
should work
You need to add
'<input type="hidden" name="id" value="' . $row['id'] . '" />'
to the form that posts to delete_process.php.
However, you should know that the code as you've posted it is horribly insecure, and it would be trivial to attack via SQL injection and delete your entire database.
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.