Provide Data from mysql using a search field in a form - mysql

i use a crm on a wordpress website. I'm trying to make a search field where a user can enter a number and with that number he/she should be able to see certain data from the database that is connected with the number that is entered.
What i got so far is:
<?php
$dbhost = 'localhost';
$dbuser = 'my user name';
$dbpass = 'my password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Kan geen connectie maken: ' . mysql_error());
}
$sql = 'SELECT lead_content FROM wp_wgbsupicrm_leads';
mysql_select_db('my database name');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Kan geen gegevens vinden: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['lead_content']} <br> ".
"--------------------------------<br>";
}
echo "Gegevens ontvangen\n";
mysql_close($conn);
?>
This will give me a result of:
resultaat:{"zoeknummer":"554477","komplex":"test
4","plaats":"84","versturen":null}
resultaat:{"zoeknummer":"556478","komplex":"test
3","plaats":"51","versturen":null}
resultaat:{"zoeknummer":"112255","komplex":"test
2","plaats":"12","versturen":null}
resultaat:{"zoeknummer":"110022","komplex":"Test
1","plaats":"1","versturen":null}
What i want is a search field where the "zoeknummer" is entered and the "komplex" & "plaats" are shown. the "versturen" isn't needed to be shown.
This is an image of the database where i need to get the information from:
enter image description here
To get this far i spended 1 and a half day.. I know i'm a total starter at this all, i have a basic html and css.. But i'm trying to learn more and hope with some assistance here i can get this to work, and in the mean time learn how it works.
Thanks in advance!

you should know how to use WHERE clause in your SQL query. this is mainly used for search purposes.
In your HTML form, give the textfield a name such as: name="$data"
Then in your SQL query:
$sql = "SELECT lead_content FROM wp_wgbsupicrm_leads' where lead_content='$data'";
Then the search will give you only the data you want from the text field you entered.

Related

How do I reset a MySQL column in a table every year?

I have a column named lv_casual in a table called tbl_employees. I need to reset the column to 0 at a specific date every year.
You can use MySQL event schedule. Providing an example below.
You have to enable the schedular first
SET GLOBAL event_scheduler = ON;
Then create the event
CREATE EVENT your_event_name
ON SCHEDULE EVERY 1 YEAR
STARTS '2021-10-12 00:00:00'
DO
UPDATE table SET column=0;
Check MySQL document for creating event
You can use a cron job to run once per year at end of the year, create a script that will reset all the records on that column to 0.
/usr/local/bin/ea-php99 /home2/accounname/https://example.com/cron_execute_file
okay bro this how your execute file should look like cron_execute_file.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection to db
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE employees SET leaves='12' WHERE leaves >= 0";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
N/B: [#Shaido][1] and any dauche bag thinking of, Please stop editing my answers, just give your own answers, adding fullstops and grammar to my answers to gain budges is a lame thing note this is not a English grammar class. Stay away from my answers, give your own answers. Polite Notice failure to I'll send some visitors to you machines.
[1]: https://stackoverflow.com/users/7579547/shaido

How to echo specific mysql data in specific places using php?

If the 'SELECT' statement is used to select data from a database then how do we echo specific rows to specific places on a page using php?
To explain this better - I am trying to SELECT * ALL FROM a table but to echo multiple rows to particular places on the html page using php.
So, imagine that my entire mark up and css has 20 thumbnails on a page and each thumbnail has data and an image that is unique to each thumbnail....do I have to replicate the below 20 times?
I am thinking that the best way to do this (which is probably completely wrong) is to use this statement
SELECT * FROM name_of_table WHERE ID = 4 >>> i.e. where I'd like that specific data echoed....
So, if I have 20 thumbnails do I do this 20 times?
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>";
}
Print "</table>";
?>
And, rinse and repeat but I change the below statement each time for each thumbnail (each thumbnail has unique data that comes from each row on the MySQL)
SELECT * FROM name_of_table WHERE ID = 4;
What is the best way of doing this?
Thanks!
Simple example.. First get the data with wanted ID:s. Create function for data request.
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);")
or die(mysql_error());
// This holds all data rows
$data_array = array();
while($info = mysql_fetch_array( $data ))
$data_array[] = $data;
// Function for rendering data to html
function getItemHtml($id) {
$html = "";
foreach($data_array as $row) {
if ($row['ID'] == $id) {
$html = "<td>" . $row['title'] . "</td>";
// etc.. create item html here
break;
}
}
return $html;
}
// To create one item just call this with item id.
echo getItemHtml(4);
?>

PHP Session Variable Not Passing with Header and Ob_Start Function

This is my code:
<?php
ob_start();
session_start();
include("index.php");
if (isset($_POST['user'],$_POST['pass'])):
$con=mysqli_connect("connect info");
if ( !$con ){
die('Could not connect to Database: '.mysqli_error());}
$pass=($_POST['pass']);
$query0 = "SELECT * FROM user WHERE username = '" . $_POST['user'] ."' AND password = '" . $pass . "'";
$resource0 = mysqli_query($con,$query0);
if(!$resource0):
die("Error conducting query. ".mysqli_error($con));
endif;
if(mysqli_num_rows($resource0) == 0){
echo "Username not find";
header("Location: /login.php");}
$result0 = mysqli_fetch_row($resource0);
$_SESSION['ID'] = $result0[0];
$_SESSION['userType'] = $result0[3];
if(!isset($_SESSION['ID'])):
//header("Location: ...");
else:
header("Location: ....");
endif;
else:
if(isset($_POST['from'])):
$_SESSION['from'] = $_POST['from'];
endif;
?>
<?php endif; ?>
This code takes a user's username and password information and stores and searching the database for the userID. Once is finds the information it stores in the session variable 'ID'.
PROBLEM: When the session 'ID' variable is passed to the next page it is not set. Surprisingly, this code without the ob_start function was working yesterday morning but wasn't working by the afternoon. I know it's not setting because two things: When I echo on the next page, nothing appears and because when I try to run a query with the session 'ID' variable I get a mysql error saying the query could not be conducted.
This is the code on the next page that is not working. At first I thought, it might be something wrong with my query because I was getting this error:
"Error conducting query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
But when I tried printing the session 'ID' variable nothing is printed.
<?php session_start;
// Create connection
$con=mysqli_connect(connect info....);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query0 = "SELECT * FROM studentProfile WHERE sID = ".$_SESSION['ID'];
$resource0 = mysqli_query($con,$query0);
if(!$resource0):
echo $_SESSION['ID'];
die("Error conducting query. ".mysqli_error($con));
endif;
.......(rest of code)
Solutions Already Tried: I have tried them all...session_write_close(), session_regenerate_id(true), session_commit(), ob_end_flush(). I have tried initially setting the session variable on the home page but once the functions are performed in the session 'ID' variable is no longer set at all.
Please help! I have read all the forums I could find on this problem but nothing seems to work.
what you have written on the next page that your code is not working. Please provide me with detail.
try this on the page which you have directed by header location. May be your session can work.
<?php
include('config.php');
if(!isset($_SESSION['ID']))
{
header("Location: ../index.html");
}
else{
$user_id=$_SESSION['ID'][0];
$user_name=$_SESSION['ID'][1];
?>
and end the else part at the last of the page

PHP login issues

I am creating a login which links to a database, when entering information the login then runs a blank page and does nothing, below is my code:
include "conn.php";
session_start();
$email_address = $_POST['email_address'];
$password = $_POST['password'];
if ($email_address && $password)
{
$connect = mysql_connect("computing","i7906890","password") or die ("couldn't connect!");
mysql_select_db("i7906890") or die ("couldn't find database");
$guery = mysql_query("SELECT * FROM UserAccount WHERE email_address = '$email_address'");
if ($numrows!=0) {
//code to login
while ($row = mysql_fetch_assoc($query)) //Password Check
{
$dbemail_address = $row['email_address']
$dbpassword = $row['password']
}
//Check if they match
if ($email_address==$dbemail_address&&$password==$dbpassword)
{
echo "You're in! <a href='user_page.php'>click</a> here to enter the members page";
$_SESSION['user']==$dbemail_address;
}
else
echo "Incorrect Password!";
}
else
die("That user doesn't exist!");
}
else
die("Please enter an email address and password!");
?>
Also here is my form
<form action = "login2.php" method ="POST">
<p><img src="images/space.gif" width="70px" height="1px"/><strong>Log in</strong> or <strong>Register</strong><br>
Email:<img src="images/space.gif" width="34px" height="1px"/><input type="text" name="user" size="33"> <br>
Password:<img src="images/space.gif" width="10px" height="1px"/><input type="password" name="password" size="33"> <br>
<div align="center">
<input type="submit" value="Log in" class="button">
</div>
</p>
</form>
Please help! SOS
You're missing a few ; in your code which is causing the script to crap out and not display anything. (Specifically in the while loop but check elsewhere as well.)
Edit: You may also want to consider losing that while loop all together and putting the password criteria in the SQL statement for better performance. And like the other poster said, watch out for SQL injection.
Please help! SOS Yep, you're in deep sh... But not for what you'd expect...
Even if your code was operating well, you are the 5th or 6th who asks roughly the same question, riddled with SQL injection in a PHP login form using the deprecated mysql_ functions...
And also, $guery is not the same as $query... Check for the q and g letters...
This line:
$guery = mysql_query("SELECT * FROM UserAccount WHERE email_address = '$email_address'");
Should be at least
$query = mysql_query("SELECT * FROM UserAccount WHERE email_address = '".mysql_real_escape($email_address)."'");
to both be correct, and avoid injection...
But you should really be using prepared statements through PDO, like this:
try {
//open connection, this is different than in the old functions
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
//***running query
//**step1: create statement
$stmt = $dbh->prepare('SELECT * FROM UserAccount WHERE email_address = :email'); //notice parameter prefixed with ':'
//**step2: bind values (be sure to also check out the bindParameter() function too!)
$stmt->bindValue(':email', $email_address);
//**step3: exexcute statement
$stmt->execute();
//**step4: process results
$result = $stmt->fetch(PDO::FETCH_OBJ);
if($result->PASSWORD==$password) {
//logged in, do whatever reuqired
}
$dbh = null; //don't let it slip out of our hands
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Also, another word of caution: don't store plaintext passwords. Even storing MD5 hashes is out of scope these days, and SHA1 is also declared to be weak...

How to remove width and height attributes from multiple posts in Wordpress (MySQL)

So I have thousands of Wordpress posts with the width and height attributes which I want to remove. For example:
img class="aligncenter size-full wp-image-21011999" title="sometitle"
alt="somealt" src="http://mysite.com/blabla/somefile.jpg" width="xxx" height="xxx"
As i mentioned i want to remove width="xxx" height="xxx" and i want to remove them directly from MySQL, dont want to use any PHP functions or similar.
Is there a query i can run through PhpMyadmin?
Are there any regex i can use for the xxx which is different for each post.
Thank you!
You can write a small PHP script (outside of WordPress of course) that will accomplish this quite easily using Regex.
<?php
$host = "hostname";
$user = "username";
$pass = "password";
$db = "database";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$pattern = "/width|height=\"[0-9]*\"/";
$query = "SELECT ID, post_content FROM wp_posts";
while ($row = mysql_fetch_assoc($query)) {
if (preg_match($pattern, $row['post_content']))
{
$row['post_content'] = preg_replace($pattern, "", $row['post_content']);
mysql_query("UPDATE wp_posts SET post_content=" . $row['post_content'] . "WHERE ID=" . $row['ID'];
}
}
?>
That's the way I'd do it anyway. I'm assuming when you said "no PHP functions" that you mean that you want the data permanently updated in the database, rather than just updated on the fly every time the page is loaded. This should solve the issue. Writing a raw SQL query to deal with this problem will likely be much more complicated.
You don't need to do this within WordPress. You could even run this on a different host, provided said host has database access.
Note: I haven't tested any of this. If you use it, I'd make sure to run it on a small subsection of your database before applying it sitewide.
I have used this script and it has some errors. I have corrected them and used it. It works. Make sure the table name (wpfs_posts) correspondents to your table name with posts. Always have a backup off course.
<?php
$host = "localhost";
$user = "nameuser";
$pass = "password";
$db = "namedb";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$pattern = "/width|height=\"[0-9]*\"/";
$query = "SELECT ID, post_content FROM wpfs_posts";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
if (preg_match($pattern, $row['post_content']))
{
$row['post_content'] = preg_replace($pattern, "", $row['post_content']);
$row['post_content']=mysql_real_escape_string($row['post_content']);
mysql_query("UPDATE wpfs_posts SET post_content='" . $row['post_content'] . "' WHERE ID=" . $row['ID']);
}
}
?>