WordPress - Select query on specific page - php

I want to select query in specific page. I created table in database:
First, I added phpexec on plugin for using PHP on page. Then, I tested Select query and it was ok. Finally, I want to create form for checking Serial Number. Here is my code:
<html>
<head>
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"><br><br>
Serial Number: <input type="Text" Name="Num1"><p>
<input type="Submit" value="Calculate">
</form>
<phpcode>
<?php
if (count($_POST) > 0 && isset($_POST["Num1"])
{
$servername = "localhost";
$username = "******";
$password = "*******";
$dbname = "******";
$serialNum = $_POST["Num1"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Install FROM SN WHERE serial = $serialNum";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["Install"];
}
} else {
echo "no result";
}
$conn->close();
}
?>
</phpcode>
</body>
</html>
Actual output is:
How to solve this issue?

Your form is broken... Try it this way
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Serial Number: <input type="Text" Name="Num1" value=""></p>
<input type="Submit" value="Calculate">
</form>

hi maybe you can try with this
is a little example and check your values and id, names for values
and sintaxis for html and php
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<br><br>
Serial Number: <input type="text" name="Num1" id="Num1">
<br><br>
<input type="submit" name="submit" id="submit" value="Calculate">
<br><br>
</form>
<phpcode>
<?php
if(isset($_POST["submit"]))
{
function Conect()
{
if (!($link=mysql_connect("localhost","username","password")))
{
echo "error to conect to database.";
exit();
}
if (!mysql_select_db("databasename",$link))
{
echo "Error to select database.";
exit();
}
return $link;
}//end function Conect
$serialNum = mysql_real_escape_string($_POST["Num1"]);
$query="SELECT Install FROM SN WHERE serial = $serialNum";
$action=mysql_query($query,$link) or die("Error: ".mysql_error());
if(mysql_num_rows($action) > 0)
{
?>
<table border="1">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>INSTALL</td>
</tr>
<?php
while($row=mysql_fetch_array($action))
{
echo "<tr>".
"<td>".$row["Install"]."</td>".
"</tr>";
}//end while
}
else
{
echo "don't exist recordsfor list ";
}//end if
mysql_close($link);
}//end if
?>
</phpcode>
</body>
</html>
good luck ..!!

Related

Registration Form Works but shows error

I made a registration form. it actually works but it shows the "Failed To Register, Try Again" message.
Here's my code.
HTML and PHP
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn">
</form>
</body>
</html>
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysql_connect("localhost","root","");
mysql_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
mysql_query($query);
echo "Registered Successfully";
}
else {
echo "Failed To Register, Try Again";
}
?>
You can check the post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
Also add uname and pass control.
<?php
if (isset($_POST['uname']) && !empty($_POST['uname']) && isset($_POST['pass']) && !empty($_POST['pass'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
............................
}
else {
echo "Failed To Register, Try Again";
}
?>
1.There is a problem with you if statement which shows failed to register on page load
Mysql is deprecated, use mysqli_*.
Here is an updated code
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysqli_connect("localhost","root","");
mysqli_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
$results=mysqli_query($query);
if(mysqli_affected_rows($con)>0){ //check if inserted
echo "Registered Successfully";
}
else{
echo "Failed To Register, Try Again";
}
}
?>
Try the below code and check
<?php
if (isset($_POST['btn'])) {
$username = $_POST['uname'];
$password = $_POST['pass'];
mysql_connect("localhost","root","");
mysql_select_db("login2");
$query = "INSERT INTO users VALUES ('','$username','$password')";
if(mysql_query($query))
{
echo "Registered Successfully";
}
else
{
echo "Failed To Register, Try Again";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="#" method="POST">
<h3>Register</h3>
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="btn">
</form>
</body>
</html>
<?php
if (isset($_POST['btn']))
{
$username = $_POST['uname'];
$password = $_POST['pass'];
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO users(`username`, `password `) VALUES ('$username','$password ')";
$result = $conn->query($sql);
if (!$result)
{
echo("Error description: " . mysqli_error($conn));
}
else
{
echo "successfully inserted";
}
}
?>

PHP GET not working

Im trying to get the taskid variable from the url:
Long story short the database never updated trying to echo $tasked is blank and im not sure why.
I have looked over all of the suggestions and many different websites I do not see what i'm missing
http://domain.com/ubxtask/addnote.php?taskid=163994
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add Note to Task</title>
</head>
<body>
<form action="" method="post">
<p>
<textarea name="notetoadd" rows="4" cols="50"></textarea>
</p>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
if ( isset( $_POST['submit'] ) ) {
$servername = "localhost";
$username = "dbusr";
$password = "dbpass";
$dbname = "db";
$notetoadd = $_POST['notetoadd'];
if (isset($_GET["taskid"])) {
//$taskid = $_GET['taskid'];
echo $_GET["taskid"];
//echo $taskid;
}
$sql = "INSERT INTO tasknotestbl (tasknum, tasknote)
VALUES ('$taskid', '$notetoadd')";
if ($conn->query($sql) === TRUE) {
header('Location: http://domain.com/task/tasklist.php');
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
You should add the task id to your forms action, or it would be lost, if you submit the form
<form action="addnote.php?taskid=<?php echo $_GET['taskid']; ?>" method="post">
You can add hidden field to form with taskid and use post method:
<?php
if (empty($_GET['taskid'])) {
$taskid = '1';
}else{
$taskid = (int)$_GET['taskid'];
}
// your code submit code and
if (isset($_POST["taskid"])) {
echo $_POST["taskid"];
}
echo '<form action="" method="post">
<p><textarea name="notetoadd" rows="4" cols="50"></textarea></p>
<input type="hidden" name="taskid" value="'.$taskid.'" placeholder="taskID">
<input type="submit" value="Submit" name="submit">
</form>';
?>

Exporting php in a same file as html to use css (PHP issue!)

my name is Luis, well Im trying to make a file in html, css and php which gets information from the database and outputs it. But it has to be in the same page as the html so I can edit with CSS... thank you all!
<html>
<head>
</head>
<body>
<script>
</script>
<form action="<?php include 'file.php';?>" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
File in PHP:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
You are including / inserting the file wrongly.
What you should do:
1) Copy your html code and paste it at the bottom of your php code i.e. after the ending '?>' tag. Your code should look like this:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
<html>
<head>
<!-- include your css here-->
</head>
<body>
<script>
</script>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
Btw, this is not the 'optimal' way to code applications. Please look into Smarty or some other templating language as well as MVC once you get the hang of php and html basics. Good luck!
To begin with, your <?php include 'file.php';?> is miss-placed and should not be in the action tag of your form rather it should be right before it, like so:
<html>
<head></head>
<body>
<?php include 'file.php';?>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>

Update database data with submit button

I want to update a database so that when you put your text in a text box and click the submit button, the data will be sent to the database with a specific id. It is clear what I want to do in the code below. When I write something like this and run it, I receive a 403 error: Access forbidden. How can I fix this?
<?php
function updater($value,$id){
// Create a connection
$conn = new mysqli( 'localhost' , 'user_name' , '' , 'data_base_name' );
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE table_name SET name=$value WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
//$conn->close();
}
?>
<!DOCTYPE html>
<html>
<header>
</header>
<body>
<form action="<?php updater($_POST['name'],1); ?>" method="post" style="height:50px;width:50px;">
<input type="text" name="name" /><br><br>
<input type="submit" /><br/>
</form>
</body>
</html>
You need to put the URL inside the action attribute that does the form processing, not the function:
action="<?php updater($_POST['name'],1); ?>" // not this
action="" // empty for the same page
Also, usually the edited value fills the input and the record's id is added to the form in a hidden field. If processing is on the same page, best to leave the action empty. So a basic form could be like this:
<form action="" method="post">
<input type="text" name="name" value="<?=htmlspecialchars($row['name']) ?>"/><br>
<input type="hidden" name="id" value="<?=htmlspecialchars($row['id']) ?>"/>
<input type="submit" /><br/>
</form>
Above the form, the processing has to be added
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$conn = new mysqli( 'localhost' , 'user_name' , '' , 'data_base_name' );
updater($conn, $_POST['name'], $_POST['id']);
}
Besides, you must use safer prepared queries:
function updater($mysqli, $value, $id) {
$sql = "UPDATE table_name SET name = ? WHERE id= ?";
$update = $mysqli->prepare($sql);
$update->bind_param('si', $value, $id);
$update->execute();
return $update->affected_rows;
}
like this:
<?php
function updater($value,$id){
// Create connection
$conn = new mysqli( 'localhost' , 'user_name' , 'pass' ,'data_base_name' );
$value =mysqli_real_escape_string($conn,$value);
$id =mysqli_real_escape_string($conn,$id);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE table_name SET name='{$value}' WHERE id='{$id}'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
if(isset($_POST['name'])){
updater($_POST['name'],$_POST['id'])
}
?>
<!DOCTYPE html>
<html>
<header>
</header>
<body>
<form action="" method="post" style="height:50px;width:50px;">
<input type="hidden" name="id" value="1" />
<input type="text" name="name" /><br><br>
<input type="submit" /><br/>
</form>
</body>
</html>

Save runtime created textbox value in database table

I have create one form in that form contain one textbox and one add another button & save button,on click event of add another button new textbox is generated but when i click on save button newly generated textbox value can not be save into database please anyone can guide me
Here is my code:
<?php
global $Hostname;
global $Username;
global $Password;
global $Database_name;
function getConnection()
{
$Hostname = "localhost";
$Username ="root";
$Password ="";
$Database_name="labdata";
$oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name); //create connection object.
return($oMysqli);
}
if(isset($_POST['submit']))
{
$TestParameters = $_POST['testparameters'];
$InsertQuery = "INSERT INTO test_table VALUES('$TestParameters')";
$oMysqli=getConnection();
$oMysqli->query($InsertQuery);
//print_r($InsertQuery);exit();
if(!$InsertQuery)
{
die('Could not enter data:' . mysql_error());
}
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row()
{
var newtr=document.createElement("tr");
var newtd=document.createElement("td");
var output="<input type=\"text\" name=\"testparameters\">";
newtd.innerHTML=output;
newtr.appendChild(newtd);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for="Testparameter">Testparameter</label>
<input type="text" name="testparameters"></input>
<table id="table1body">
<tr>
<td><input type="button" name="button" value="Add Test Parameter" onclick="create_row()">
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Try this
if(isset($_POST['submit']))
{
$length = count($_POST['testparameters']);
for($i=0; $i< $length; $i++){
$TestParameters=$_POST['testparameters'][$i];
if(!empty($TestParameters)){
$InsertQuery = "INSERT INTO color(name) VALUES('$TestParameters')";
$result=mysql_query($InsertQuery) or die(mysql_error());
}
}
if(!$InsertQuery)
{
die('Could not enter data:' . mysql_error());
}
}
<?php
$n=0;
if(isset($_GET['button1']))
{
$n++;
echo "hai";
echo "<br><input type='text' name='+i+'>";
}
if(isset($_POST['submit']))
{
$con=mysqli_connect("localhost","root","","ram123");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//print_r($_POST);
$count = count($_POST);
for($x=1;$x<$count;$x++){
$sql="INSERT INTO names (name)VALUES('$_POST[$x]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
/*
$sql="INSERT INTO names (name)VALUES('$_POST[1]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
*/
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
var i = 1;
function changeIt()
{
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name="+i+">";
i++;
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<input type="button" name="button1" value="Add Test Parameter" onclick="changeIt()">
<input type="submit" name="submit" value="submit">
<div id="my_div"></div>
</form>
</body>
</html>
you are using same name for all textbox
<input type="text" name="testparameters"></input>
use array for this,from this you will get value of all your textbox
<input type="text" name="testparameters[]"></input>
Code for iterating textbox value
$length = count($_POST['testparameters']);
for($i=0; $i< $length; $i++){
echo $_POST['testparameters'][$i];
}

Categories