I have obviously seen other threads related to the matter and couldn't solve my problem. I am not experienced with php. My file structure is like:
-search/
-index.jade
-search.php
css/
index.css
My index.jade code:
html
head
script(src='https://code.jquery.com/jquery-3.1.0.min.js')
link(href="css/index.css", rel="stylesheet", type="text/css")
body
form(id="search", method="POST", action="search.php")
input(type="text",placeholder="Search for...",name="search_bar")
input(type="submit",name="submit")
My search.php code:
<?php
$SQL_HOST = "peanuts";
$SQL_PSWD = "more_peanuts";
$SQL_USER = "peanuts_again";
$SQL_DB = "no_peanuts";
// jk...
try {
$link = new mysqli($SQL_HOST, $SQL_USER, $SQL_PSWD, $SQL_DB);
} catch (Exception $e) {
echo "PDO connection error: " . $e->getMessage();
exit(1);
}
$search = $_POST['search_bar']."*";
$search_query = $link->prepare("SELECT name FROM products WHERE MATCH(name) AGAINST (? IN BOOLEAN MODE)");
$search_query->bind_param('s', $search);
$search_query->execute();
$search_query->store_result();
$search_rows = $search_query->num_rows;
$search_query->bind_result($product_name);
if($search_rows > 0) {
while($search_query->fetch()) {
echo "Your search returned $search_rows results";
echo "$product_name <br>";
}
}
else {
echo "Your search returned no results, sorry :(";
}
?>
I keep getting: "Cannot POST /search/search.php". Help?
Related
After executing the SQL file below to create the appropriate tables(which gets executed correctly without errors),
<?php
try {
require_once 'dbcon.php';
$sql_file = 'mysql.sql';
$contents = file_get_contents($sql_file);
$comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/',
'/\s*--.*\n/',
'/\s*#.*\n/',
);
$contents = preg_replace($comment_patterns, "\n", $contents);
$statements = explode(";\n", $contents);
$statements = preg_replace("/\s/", ' ', $statements);
foreach ($statements as $query) {
if(trim($query) != '') {
$db->query($query);
if ($db->errno) {
throw new Exception("Fail to load data in database (".$db->errno.")");
}
}
}
Running the following query right after the foreach gets executed successfully without errors and success message gets printed. However , no data gets inserted into the database.
$cql = "INSERT INTO config (logo,brand,provider,mail_type,url) VALUES(?,?,?,?,?)";
$cstmt = $db->prepare($cql);
$sch_logo = 'logo.png';
$sch_brand = 'brand.png';
$provider = 'other';
$mail_type = 'mail';
$cstmt->bind_param('sssss',$sch_logo,$sch_brand,$provider,$mail_type,$site_url);
$cstmt->execute();
if($cstmt->affected_rows === 1){
echo 'Identity verified. Thank you';
}
else{
throw new Exception("An error occurred Performing this operation.");
}
}
catch(Exception $e) {
error_log($e->getMessage());
echo ' <div class="text-warning">
<b>'.$e->getMessage().'</b> </div>';
exit();
}
?>
I have printed out $cstmt->error and $cstmt->errno and they all return 0 . which seems fine but don't why the data doesn't get inserted into the fields. Anything am missing or doing wrong?
Try this:
$cql = "INSERT INTO config (logo,brand,provider,mail_type,url) VALUES(?,?,?,?,?)";
$cstmt = $db->prepare($cql);
$sch_logo = 'logo.png';
$sch_brand = 'brand.png';
$provider = 'other';
$mail_type = 'mail';
$cstmt->bind_param('sssss',$sch_logo,$sch_brand,$provider,$mail_type,$site_url);
**if($cstmt->execute()){**
echo 'Identity verified. Thank you';
}
else{
throw new Exception("An error occurred Performing this operation.");
}
}
catch(Exception $e) {
error_log($e->getMessage());
echo ' <div class="text-warning">
<b>'.$e->getMessage().'</b> </div>';
exit();
}
when executing the form, the data is not inserting. I have activated SQLITE3 and I am not skipping any type of error.
The echo of the try is to see what was wrong but nothing. I see everything right.
Does anyone help me?
$username = $_POST['nombre'];
$clave = $_POST['clave'];
$apenom = $_POST['apenom'];
try {
$bd = new SQLite3("test");
//preparamos la sentencia
echo "INSERT INTO usuarios (username,clave,apenom) VALUES ('$username','$clave','$apenom')";
$bd->exec("INSERT INTO usuarios (username,clave,apenom) VALUES ('$username','$clave','$apenom')");
/* while ($row = $resultado->fetchArray()) {
echo "{$row['username']} {$row['clave']} {$row['apenom']} \n";
} */
} catch (\Throwable $th) {
echo $th;
}
Do take the advice from the comments into account, database security is not something you should 'wing'...
As for a little help on setting up a connection and importantly debugging if anything goes wrong so you know what to fix, the following 'skeleton' might help:
<?php
try {
// connect to your database
$sqlite = new SQLite3('test.db');
}
catch (Exception $e) {
// if no connection could be established a exception is thrown
echo $e->getMessage();
}
// your query
$query = '...';
$result = $sqlite->query($query); // result object (FALSE on error)
if (!$result) {
// query failed for some reason...
echo $sqlite->lastErrorMsg();
} else {
// do something with result
}
I am trying to get a mysql data from the table, here -
try
{
$stmt = $user->prepare("SELECT status FROM users");
$result=$stmt->fetch(PDO::FETCH_ASSOC);
if($result['status'] != "Y")
{
$error[] = "Some error warning!";
}
else
{
// Some php codes
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Here user is a class where prepare is db connection mysql prepare function. The error always prints - "Array!". I am new to php. Any help will be appreciated.
EDIT: I have managed to solve the problem.
You forgot the call of PDOStatement::execute(). See php.net for some examples.
Have you already tried this?
try
{
$stmt = $user->prepare("SELECT status FROM users");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result['status'] != "Y")
{
$error[] = "Some error warning!";
}
else
{
// Some php codes
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Regarding the Array! output: Did you post the whole code of your script? Were do you try to print the array $error?
This question already has answers here:
Row count with PDO
(21 answers)
Closed 6 years ago.
I'm not managing to display a message nothing found when the table is empty this query, someone could say where I am going wrong?
<?php
$sqlRead = "SELECT * FROM dados ORDER BY name ASC";
try{
$read = $db->prepare($sqlRead);
$read->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
while ($rs = $read->fetch(PDO::FETCH_OBJ)){
?>
<h1><?php echo $rs->name; ?></h1>
I found the solution
<?php
$sqlRead = "SELECT * FROM dados ORDER BY name ASC";
try{
$read = $db->prepare($sqlRead);
$read->execute();
$result = $read->rowCount();
} catch (PDOException $e) {
echo $e->getMessage();
}
if($result == 0){
echo "NOTHING FOUND!";
} else {
while ($rs = $read->fetch(PDO::FETCH_OBJ)){
?>
<?php echo $rs->name; ?>
<?php } } ?>
With the code above, can display the message if nothing is found in
the database
Hi I am facing a unique issue with my PHP script.Here is my code. After writeToDB() is executed I dont see the echo ("<script> top.location.href=www.facebook.com</script>");
Can someone let me know why my script stops executing after writing to db?
<?php
function writeToDB($access_token,$uid,$username,$birthday,$gender,$age)
{
/* Database Connection */
$user = "xxxx";
$password = "xxxx";
$host = "xxxxxxxxxxxxxxxxxx";
//connect to database, where tsnames.ora is setup
$connect_obj = oci_connect($user, $password, $host);
if ($connect_obj) {
error_log("connected okay");
} else {
$err = OCIError();
echo "Oracle connection error " . $err[text];
return;
}
$select_query = "SELECT USER_ID FROM FBTABLE WHERE USER_ID= '$uid'";
$select_sql_stmt = oci_parse($connect_obj, $select_query);
//execute statement
try {
$r = oci_execute($select_sql_stmt, OCI_DEFAULT);
if (!$r) {
$p = oci_error($select_sql_stmt);
echo "Oci Execute error";
}
} catch (Exception $e) {
echo "<br>Failed to get database info" . $e->getMessage();
}
$user_id_in_db = null;
while (oci_fetch($select_sql_stmt)) {
$user_id_in_db = oci_result($select_sql_stmt, 'USER_ID');
}
// User already exists in db so update instead of insert
if ($user_id_in_db != null) {
$query ="UPDATE FBTABLE SET ACCESS_TOKEN='$access_token' WHERE USER_ID='$uid'";
} else {
$query = "INSERT INTO FBTABLE(ACCESS_TOKEN, USER_ID,USER_NAME,BIRTHDAY,GENDER,AGE)
VALUES
('$access_token','$uid','$username','$birthday','$gender','$age')";
}
//create sql statement
$sql_statement = oci_parse($connect_obj, $query);
//execute statement
try {
$r = oci_execute($sql_statement, OCI_DEFAULT);
if (!$r) {
$p = oci_error($sql_statement);
echo "Oci Execute error";
}
} catch (Exception $e) {
echo "<br>Failed to get database info" . $e->getMessage();
}
//Commit transaction
$committed = oci_commit($connect_obj);
//Test whether commit was successful. If error occurred, return error message
if (!$committed) {
$error = oci_error($conn);
echo 'Commit failed. Oracle reports: ' . $error['message'];
}
//close the connection
$oci_free_statement($sql_statement);
if (oci_close($connect_obj)) {
echo " oci connection not closed!!!";
}
//close the connection
$oci_free_statement($sql_statement);
}
?>
<html>
<body>
<?php
$access_token = $_GET['access_token'];
$uid = $_GET['uid'];
$username = $_GET['username'];
$birthday = $_GET['birthday'];
$gender = $_GET['gender'];
$age = $_GET['age'];
echo $username;
writeToDB($access_token,$uid,$username,$birthday,$gender,$age);
echo ("<script> top.location.href=www.facebook.com</script>");
?>
</body>
</html>
i think error is in $oci_free_statement($sql_statement); must be oci_free_statement($sql_statement); extra $ before oci_free_statement
http://php.net/manual/en/function.oci-free-statement.php
no any error show because of error_display is off
Your JavaScript code should be
echo ("<script> top.location.href='http://www.facebook.com';</script>");
It's happen because writeToDB() causes error. You don't see this error because error_display is off or error_reporting = 0
Also maybe you didn't install OCI8. So when you call oci_connect it will cause error.
Thanks.
you are not using quotes around the string:
www.facebook.com should be 'www.facebook.com'