how to display array values in php? - php

im performing search from the database and displaying the result in the checkbox format in which when i select the checkbox the respectively values should be inserted in the another table which is in (a.php) since the search result have multiple values im storing the values in array but when im trying print the array with echo $_POST['friend']; it showing result as "Array" anyone help me how can i display the variables stored in array
<form method="post">
<div class="form-group">
Name
<br/>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
Email <br/>
<input type="text" class="form-control"name ="email" />
</div>
<div class="form-group">
Qualification<br/>
<input type="text" class="form-control" name ="qualify" />
</div>
<input type="submit" value="Search" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name=$_POST['name'];
$email=$_POST['email'];
$qualification=$_POST['qualify'];
$sql = "SELECT * FROM form WHERE Name ='$name' OR EmailAddress = '$email' OR Qualification = '$qualification' ";
$result=$conn->query($sql);
if(!empty($_POST)) {
if($result->num_rows === 0)
{
echo '<p style="margin-left:340px">no records</p>';
}
}
while($row = $result->fetch_assoc())
{
//$_SESSION["snum"]=$row['sno'];
//$_SESSION["nam"]=$row['Name'];
//$_SESSION["quali"]=$row['Qualification'];
// $_SESSION["emai"]=$row['EmailAddress'];
echo '<br>';
echo '<form name="friend" action="a.php" method="post">';
echo '<input style="margin-left:340px;padding-bottom:10px" type="checkbox" name="friend[]"> user Details</input>';
echo '<br>';
echo '<br>';
echo '<div class="container" style="border-style:solid; border-width:medium;width: 550px;">';
echo '<br>';
echo 'Name: '.$row['Name'];
echo '<br /> EmailAddress: ' .$row['EmailAddress'];
echo '<br /> Qualification: '.$row['Qualification'];
echo '<br /> DOB: '.$row['DOB'];
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '</div>';
echo '<br/>';
}
echo '<button type ="submit">invite</button>';
echo '</form>';
$conn->close();
?>

Try this:
1.The print_r(variable); function is used to print human-readable information about a variable.
2.The var_dump(variable); function is used to display structured information (type and value) about one or more variables.

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$count=count($_POST['friend']);
for($i=0; $i<$count; $i++)
{
$a=$_POST['friend'][$i];
//echo $a;
$sql = "SELECT Name,EmailAddress,Qualification FROM form WHERE sno='$a'";
$result=$conn->query($sql);
while($row = $result->fetch_assoc()){
$ab=$row['Name'];
$bc=$row['EmailAddress'];
$ca=$row['Qualification'];
echo $ab;
echo'<br/>';
echo $bc;
echo'<br/>';
echo $ca;
echo'<br/>';
$sql1="INSERT INTO arun ". "VALUES('$ab')";
$result1=$conn->query($sql1);
}
}
if (!$result) {
//$_SESSION['message10'] = '<p style="color:green;margin-left: 250px";>Your request has been send </p>';
//header("Location:send.php");
echo "not send";
}
else {
// $_SESSION['message11'] = '<p style="color:red;margin-left: 250px";>you have already send request to the user</p>';
//header("Location:new.php");
echo "send";
}
$conn->close();
?>

Related

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

when i insert data in db i get num1 in all column

i try to creat a table with html and php
when i insert data into my db i get num 1 like a values in all column
this my code
<html dir="rtl">
<form action="" method="post">
<label for="Nom">الاسم:</label>
<center><input type="text" name="Nom"></center>
<label for="Cin">البطاقة الوطنية:</label>
<center><input type="text" name="Cin"></center>
<label for="Tel">الهاتف:</label>
<center> <input type="text" name="Tel"></center>
<label for="DATE_donation"> تاريخ التبرع:</label>
<center><input type="date" name="DATE_donation"></center>
<center><input type="submit" value="إدخال"></center>
</form>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ikhlas";
$con= mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$Name =isset($_POST['Nom']);
$CIN = isset($_POST['Cin']);
$TEL = isset($_POST['Tel']);
$date = isset($_POST['DATE_donation']);
$sql="INSERT INTO persone(Nom, Cin, Tel, DATE_donation) value ('$Name','$CIN','$TEL','$date')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
?>
and this my ressult in dbenter image description here
Sure because you define all variable with $foo = isset($bar) instead of
if(isset($bar))
$foo = $bar;
Take a look to the doc about SQL injection too: http://php.net/manual/en/security.database.sql-injection.php
Remove your isset()
http://php.net/manual/en/function.isset.php
Replace to:
if(isset($_POST['إدخال']))
{
$Name = (!empty($_POST['Nom']))?$_POST['Nom']:"";
$CIN = (!empty($_POST['Cin']))?$_POST['Cin']:"";
$TEL = (!empty($_POST['Tel']))?$_POST['Tel']:"";
$date = (!empty($_POST['DATE_donation']))?$_POST['DATE_donation']:"";
}

Upload file in a form containing multiple textfields using PHP

I'm trying to figure out how to upload a file into the database where that form contains multiple textfields. I uploaded a BLOB field into the database. So as I try to search the field using the ID number, it will retrieve me the values associated with it. Which works fine, so I added the function of being able to upload a file into that specific id number. I get all sorts of errors and I would like to have an assistance with it. Anyone care to help out? Here are the codes:
<?php
$host = "localhost";
$user = "root";
$password ="";
$database = "ntmadb";
$id = "";
$firstname = "";
$lastname = "";
$username = "";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// connect to mysql database
try{
$connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
echo 'Error';
}
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['id'];
$posts[1] = $_POST['firstname'];
$posts[2] = $_POST['lastname'];
$posts[3] = $_POST['username'];
return $posts;
}
// Search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query = "SELECT * FROM members WHERE id = $data[0]";
$search_Result = mysqli_query($connect, $search_Query);
if($search_Result)
{
if(mysqli_num_rows($search_Result))
{
while($row = mysqli_fetch_array($search_Result))
{
$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$username = $row['username'];
}
}else{
echo 'No Data For This Id';
}
}else{
echo 'Result Error';
}
}
// Edit
if(isset($_POST['update']))
{
$data = getPosts();
$update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
try{
$update_Result = mysqli_query($connect, $update_Query);
if($update_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
echo 'Data Updated';
}else{
echo 'Data Not Updated';
}
}
} catch (Exception $ex) {
echo 'Error Update '.$ex->getMessage();
}
}
<!--UPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOAD -->
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'ntmadb');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
// Create the SQL query
$query = "
INSERT INTO `members` (
`data`
)
VALUES (
'{$data}' NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
?>
and here is the html file:
<!DOCTYPE Html>
<html>
<head>
<title>PHP INSERT UPDATE DELETE SEARCH</title>
</head>
<body>
<form action="index4.php" method="post" enctype="multipart/form-data" >
<input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname;?>"><br><br>
<input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname;?>"><br><br>
<input type="text" name="username" placeholder="User Name" value="<?php echo $username;?>"><br><br>
<div>
<p>
<!-- Input For Edit Values -->
<input type="submit" name="update" value="Update">
<!-- Input For Find Values With The given ID -->
<input type="submit" name="search" value="Find">
</p>
<p>
<input type="file" name="uploaded_file">
<br>
<input type="submit" value="Upload file">
</p>
</div>
</form>
</body>
</html>
Thanks to anyone who can provide me with help. :)

data is not insert to my database table

<?php
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo 'not connected to server';
}
if(!mysqli_select_db($con,'user_table'))
{
echo 'database not selected';
}
$mobilenumber = $_POST['mobilenumber'];
$operator = $_POST['operator'];
$state = $_POST['state'];
$sql = "INSERT INTO user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."','".$operator."','".$state."')";
if(!mysqli_query($con,$sql))
{
echo 'not inserted';
}
else
{
echo 'inserted';
}
//$select="insert into user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."".$operator."".$state."')";
//$sql=mysql_query($select);
//print '<script type="text/javascript">';
//print 'alert("the data is inserted.....")';
//print '<script>';
//mysql_close();
?>
<form method="post" action="insert.php" id="submitForm">
<select id="operator" type="select" ng-model="operatorName" ng-class="operatorError ? 'error-border':''" autocomplete="off" placeholder="Select operator" ng-click="showOperators($event);" name="operator" method=POST action=/insert.php class="form-control fetchoperators drpdwn-arrow ng-pristine ng-valid ng-touched" required>
<select name="state" class="state" id="state" required name="state" method="post" action="insert.php">
The Simplest way is to try to debug your query.
What i would have done is simply the process as much as i could firt.
do echo $sql;
Then copy the result and paste it in the my sql server if that works.
Then check your connection object apparently it's look right but the best way of doing this copy the following code. and try this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user_details (mobilenumber,operator,state) VALUES ('".$mobilenumber."','".$operator."','".$state."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
change the credentials and check the response and paste it in the comments then we will go along.
you should use
mysqli_query($con,$sql) or die((mysql_error());
it will show you error for which it is not inserting data in you table

i cant input values into a form directly from my data in the database

how do i get the value in the form input to be equal to the data fetched from my database . when i tried to retrieve the value from the post super global i got Array ( [john_doe] => ) i.e an empty value
<?php print_r($_POST); ?>
<form action="" method="post">
<?php while($subs = mysqli_fetch_assoc($result)) { ?>
<input type="" name="john_doe" value='<?php echo $subs["menu_name"]; ?>'>
<input type="submit" value="sumbit">
<?php } ?>
</form>
There will be some wrong with your database fetching, you can try this following code for your purpose
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "**YOUR QUERY**";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
<input type="text" name="john_doe" value='<?php echo $row["menu_name"] ;?>'>
<input type="submit" value="sumbit">
$conn->close();
?>

Categories