Issue with webform used for adding and deleting customer names - php

I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.
I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.
Here's my index.php file:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url from customers";
$result = $conn->query($sql);
$tempArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tempArray[] = $row["url"];
}
} else {
echo "0 results";
}
$conn->close();
?>
<table>
<tr>
<td><u>URL</u></td>
<td><u>Action</u></td>
</tr>
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
</tr>
<?php endforeach; ?>
</table>
<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>
</body>
</html>
Here's my add_customers.php file:
<html> <body>
Added <?php echo $_GET['url']; ?><br>
<?php
$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";
// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "INSERT INTO customers (url) VALUES ('$url')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close(); ?>
</body> </html>
Here's my disable_customer.php file:
<html>
<body>
<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;
$url = $_GET['url'];
echo "<br>" . $url . "<br>";
$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}
$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
echo "Record successfully deleted.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>

try changing index.php from this:
<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>
to this:
<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>
If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:
Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
</tr>
<?php endforeach; ?>

Related

PHP PDO output in angular

I am trying to get output in Angular via PHP PDO but unable to understand the error.
When I write the same code using PHP procedural way (mysqli) then I can easily fetch the output. But I am struggling in fetching data with PHP PDO.
Here is my code:
Index.php:
<div ng-app="myapp" ng-controller="fieldcontroller" ng-init="displayData()">
<tr ng-repeat="field in fields">
<td>{{ field.fieldlabel }}</td>
</tr>
My JS File:
var app = angular.module("myapp",[]);
app.controller("fieldcontroller", function($scope, $http){
//Display Function
$scope.displayData = function(){
$http.get("model/select.php?type=getForm")
.then(function(response) {
$scope.fields = response.data;
console.log($scope.fields);
});
}
});
PHP File:
?php
//include('Database.php');
//select.php
//$connect = mysqli_connect("localhost", "kcmsuser", "KC+wSH&X#z9P", "kcms");
$server = 'localhost';
$user = 'kcmsuser';
$pass = 'KC+wSH&X#z9P';
$dbname = 'kcms';
$conn = new mysqli($server, $user, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo " Connected successfully ";
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$output = array();
while($row = $result->fetch_array()) {
$output[] = $row;
}
} else {
echo "0 Results ";
}
echo json_encode($output);
Console.log shows:
[{"0":"110","id":"110","1":"First Name","fieldlabel":"First Name","2":"firstname","fieldname":"firstname","3":"text","fieldtype":"text","4":"","isprimary":"","5":"yes","required":"yes","6":"1","position":"1"},{"0":"23","id":"23","1":"Carrier","fieldlabel":"Carrier","2":"carrier","fieldname":"carrier","3":"text","fieldtype":"text","4":"no","isprimary":"no","5":"yes","required":"yes","6":"5","position":"5"},{"0":"26","id":"26","1":"Email","fieldlabel":"Email","2":"email","fieldname":"email","3":"email","fieldtype":"email","4":"","isprimary":"","5":"yes","required":"yes","6":"9","position":"9"},{"0":"27","id":"27","1":"Password","fieldlabel":"Password","2":"password","fieldname":"password","3":"password","fieldtype":"password","4":"","isprimary":"","5":"no","required":"no","6":"4","position":"4"},{"0":"102","id":"102","1":"Date of Birth","fieldlabel":"Date of Birth","2":"dob","fieldname":"dob","3":"date","fieldtype":"date","4":"","isprimary":"","5":"no","required":"no","6":"6","position":"6"},{"0":"101","id":"101","1":"Last Name","fieldlabel":"Last Name","2":"lastname","fieldname":"lastname","3":"text","fieldtype":"text","4":"","isprimary":"","5":"yes","required":"yes","6":"2","position":"2"}]
And [ngRepeat:dupes] warning.
I tried track by $index with ng-repeat but nothing happens.
I am new to Angular.
you can use pdo code for get data from mysql
<?php
$servername="localhost";
$username="";
$password="";
$dbname="";
$dsn="mysql:host=$servername;dbname=$dbname";
try{
$connect=new PDO ($dsn,$username,$password);
$connect->exec("SET NAMES 'utf8';");
}catch(PDOException $error){
echo "Error in connect".$error->getMessage();
exit();
}
$sql = "SELECT * from `table`";
$result = $connect->query($sql);
$num_row=$connect->query("SELECT count(id) from `table`")->fetchColumn();
if ($num_row > 0) {
$output = array();
while($row=$result->fetch(PDO::FETCH_ASSOC)) {
$output[] = $row;
}
} else {
echo "0 Results ";
}
?>
After spending few hours and hit and try message, I was able to sole the problem.
Problem was in the below PHP code:
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo " Connected successfully ";
}
Thanks....
sorry i was busy, i write code for you very fast if you have any proplem you can say me
<?PHP
if(#$_REQUEST['delete']){
/**************delete**************/
$delete=$_REQUEST['delete'];
if($connect->query("delete from table where id='$delete'")){
echo 'delete successful';
}}else if(#$_REQUEST['insert']=='record'){
/**************insert**************/
if(isset($_POST['submit'])){
$title=$_POST['title'];
if($connect->query("INSERT INTO `table`(`title`) VALUES ('$title')")){
echo 'success'; }else{ echo 'error'; }
}}else if(#$_REQUEST['edit']=='record'){
/**************insert**************/
if(isset($_POST['submit'])){
$title=$_POST['title'];
$mid=$_POST['mid'];
if($connect->query("UPDATE `table` SET `title`='$title'")){
echo 'success'; }else{ echo 'error'; }
}} ?>
<h1><?PHP if(#$_REQUEST['edit']=='new'){?>insert<?PHP }else if(#$_REQUEST['edit']=='new'){?>edit<?PHP }else{?>manage<?PHP }?><h1>
<br>insert
<?PHP
if(#$_REQUEST['edit']=='new'){
$id=$_REQUEST['id'];
$checks=$connect->query("select * from table where id='$id' order by id desc");
$check=$checks->fetch(PDO::FETCH_ASSOC);
?>
<form>
<input type="text" name="title" value="<?=$check['title']?>">
<input type="hidden" name="edit" value="record">
<input type="hidden" name="mid" value="<?=$check['id']?>">
<input type="submit" value="edit" name="submit"/>
</form>
<?PHP }else if(#$_REQUEST['insert']=='new'){?>
<form>
<input type="text" name="title">
<input type="hidden" name="insert" value="record">
<input type="submit" value="create" name="submit"/>
</form>
<?PHP
}else{
$radif=0;
$qacs=$connect->query("select * from table order by id desc");
while ($qc=$qacs->fetch(PDO::FETCH_ASSOC)){$radif++;
?>
<tr>
<td scope="row"><?=$radif?></td>
<td><?=$qc['title']?></td>
<td>delete</td>
<td>edit</td>
<?PHP }}?>

How to pass posted variables to an Anchor ID tag in PHP

I have been trying to insert some data into a table in DB, but the error came at where one of my fields is GET variable from the anchor tag.
To get a clear understanding please take a look at my code:
index page: with a post value of id = 1
<form name="user_form" method="post" action="input.php">
<?php
$sql2 = "SELECT * FROM singleq ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$id = $row2['id'];
$cat = $row2['cat'];
?>
<tr >
<input type="hidden" name="q_id" id="q_id" value="<?php echo ($row2["id"]); ?>" />
<input type="hidden" name="tablum" id="tablum" value="singleq" />
<input type="hidden" name="test_id" id="test_id" value="<?php echo $_GET['id'] ?>" />
<td><a href='singleqbanksub.php?id=<?php echo $row2['id'];?>'><?php echo substr(($row2["question"]),0,100) ?>...</a></td>
<td><button class="button button1" type="submit" name="insert-data" id="insert-data" style="width:180px;">Add Question</button></td>
</tr>
<?php }
} else {
echo "";
}
?>
</form>
input.php :
<?php include('db.php'); ?>
<?php
$q_id = $_POST['q_id'];
$test_id = $_POST['test_id'];
$tablum = $_POST['tablum'];
$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO english (cat, test_id, q_id, tablum)
VALUES ('$cat', '$test_id', '$q_id', '$tablum')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php?id='echo "$test_id";'');
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here as I tried to pass the id through the anchor tag redirection to the same page to loop the process, it shows an error about an unidentified echo.
You are using echo inside header function which is wrong , use like this:
header('Location: index.php?id='.$test_id);

PHP web form, used to add/remove customer names, not working correctly [duplicate]

I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.
I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.
Here's my index.php file:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url from customers";
$result = $conn->query($sql);
$tempArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tempArray[] = $row["url"];
}
} else {
echo "0 results";
}
$conn->close();
?>
<table>
<tr>
<td><u>URL</u></td>
<td><u>Action</u></td>
</tr>
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
</tr>
<?php endforeach; ?>
</table>
<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>
</body>
</html>
Here's my add_customers.php file:
<html> <body>
Added <?php echo $_GET['url']; ?><br>
<?php
$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";
// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "INSERT INTO customers (url) VALUES ('$url')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close(); ?>
</body> </html>
Here's my disable_customer.php file:
<html>
<body>
<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;
$url = $_GET['url'];
echo "<br>" . $url . "<br>";
$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}
$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
echo "Record successfully deleted.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
try changing index.php from this:
<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>
to this:
<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>
If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:
Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
</tr>
<?php endforeach; ?>

Database search returns inconsistent results

I'm running a simple database search against one table in my database. The results are then displayed in a table. If no results are found, the search shows a message that says "0 results", but sometimes it will echo the table headings without displaying any results. This search is part of a class project and isn't going to be an active database, so I haven't included any protection for SQL injection. Any help would be greatly appreciated.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
while ($row = mysqli_fetch_row($result)) {
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
}
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>
I find, and you may disagree, but properly indenting your code as you go and opening and closing tags before filling in content help reduce the number of problems. Having said that now it'll probably not work!
<html>
<head>
<title>db search</title>
</head>
<body>
<div>
<div>
<h2>Customer Search</h2>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
$conn = new mysqli( $servername, $username, $password, $dbname );
if ( $conn->connect_error ) die("Connection failed: " . $conn->connect_error );
if( isset( $_POST['search'] ) ) {
$searchq = $_POST['search'];
$searchq = preg_replace( "#[^0-9a-z]#i", "", $searchq );
$sql = "SELECT * FROM `Customers` WHERE `Client` LIKE '%$searchq%';";
if ( $result = mysqli_query( $conn, $sql ) ) {
if ( mysqli_num_rows( $result ) > 0 ) {
echo '
<table class="hoverTable">
<tr>
<th>Client</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Phone</th>
</tr>';
while( $row = $result->fetch_assoc() ) {
echo "
<tr>
<td>".$row["Client"]."</td>
<td>".$row["Address"]."</td>
<td>".$row["City"]."</td>
<td>".$row["State"]."</td>
<td>".$row["ZipCode"]."</td>
<td>".$row["Phone"]."</td>
</tr>";
}
echo '
</table>';
} else {
$message = "0 results";
}
}
mysqli_free_result( $result );
}
?>
</div>
</div>
<div class="center">
<?php if( isset( $message ) ){ echo $message; } ?>
</div>
</body>
</html>
Delete the redundant first while loop while ($row = mysqli_fetch_row($result)) {, if the query had just 1 result the first loop would have fetched it only while the 2nd while loop would have fetched nothing resulting in just a header and no body of the table.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>

Why this is not updating Information

Why this code is not update information?
HTML Form:
<form>
<lable> ID# :</lable>
<input id= "ID" name= "ID" type= "text">
<p>
<label>Select field to Edit</label>
<select name="change">
<option value=""></option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
<option value="email">Email</option>
<option value="city">City</option>
<option value="zip">Zip</option>
</select>
<lable> Enter the value to be replaced </label>
<input id = "replace" name = "replace" type = "text">
</p>
<input name="submit" type="submit" value="Submit">
PHP Code for updating information from database:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = mysql_connect($servername,$username,$password);
if(!$conn)
{
die('Error!' . mysqli_error());
}
$sql = 'SELECT * FROM users';
mysql_select_db('mitsdatabase');
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not get data:' . mysql_error());
}
echo "<table width='300' cellpadding='5' border='1'>";
echo "<tr> <td>ID#</td> <td>FirstName</td> <td>LastName</td> <td> Email </td> <td> City </td> <td> State </td> <td> Zip </td> </tr>";
while($row = mysql_fetch_array($retval,MYSQL_ASSOC))
{
echo "<tr> <td>{$row['ID']}</td> . <td>{$row['fname']}</td> . <td>{$row['lname']}</td> . <td>{$row['email']}</td> . <td>{$row['city']}</td> . <td>{$row['state']} </td>. <td>{$row['zip']}</td>";
}
echo "</table>";
$db_id = $_POST['ID'];
$db_select = $_POST['change'];
$db_replace= $_POST['replace'];
echo " Do you want to edit any entry?";
if(!_POST['submit'])
{
echo " ";
}
else{
mysqli_query("UPDATE users SET db_select='$db_replace' WHERE ID = $db_id ");
}
mysql_close($conn);
?>
I want to update informate selected from select field but somehow it is not doing any thing. Can someone help me what is wrong with this code.
Is your PHP on the same page as your HTML? If not, you are not directing to your php code within the <form> element in your HTML.
For example, if your PHP file was called 'myphpcode.php' (and in the same folder as your HTML code) then you could direct to it using the following:
<form method="post" action="myphpcode.php">
If you want to post to the same page just change <form> to <form method="post" action="#"> and get variables in php like this $nameofvar = $_POST['nameofinputfield'] . Each input field should have the name tag.
Also try to change your mysql connect to this :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
and after you finished the query
$conn->close();
and the query to insert
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
and you can modify this $sql string to update or delete

Categories