I have a question that, I would have thought has definitely been asked before so excuse me if I am phrasing it wrong but I was unable to find it. The question requires a few separate snippets of code to address. I have an application that we use to review the validity of leads submitted to our client from certain forms for billing purposes. What I am wanting, is for when the user presses the button "valid" or "invalid", it assigns the lead the proper status and removes it from the currently displayed table, which would be leads that 'need review'. These leads have a needsreview value of 'null', which is defaulted. The other leads are assigned an appropriate value of 1 or 0 upon the button press.
That all works, the only thing I cannot figure out is how to not redirect the user to restart the search on click. I just want it to remove the record from what is currently displayed. Here is a few pieces of the code, I would greatly appreciate any help. Again, it all works, just in not the most convenient way and I am not sure how to do it the better way, I just need someone more experienced to point me in the right direction.
Here is the buttons code:
<td style="background:#fff;border:none;">
<button style="margin-bottom:2.5%;margin-top:2.5%;background:#2ECC71;border-radius:5px;width:175px;height:30px;border:3px solid #3FC380;color:#fff;font-weight:bold;" onclick="location='admin.php?action=validCustomer&customersId=<?php echo $customers->id?>'">Valid</button>
</td>
<td style="background:#fff;border:none;">
<button style="margin-bottom:2.5%;margin-top:2.5%;background:#2ECC71;border-radius:5px;width:175px;height:30px;border:3px solid #3FC380;color:#fff;font-weight:bold;" onclick="location='admin.php?action=invalidCustomer&customersId=<?php echo $customers->id?>'">Not Valid</button>
</td>
Here is the other relevant code:
function searchIsValid() {
$results['formAction'] = "searchIsValid";
$results['pageTitle'] = "Search By Validation Status";
require (TEMPLATE_PATH . "/admin/searchIsValid.php" );
}
function validCustomer(){
if (!$customers = customers::getById( (int)$_GET['customersId'] ) ){
echo "Error getting customer ID";
}
$results['formAction'] = "validCustomer";
$results['pageTitle'] = "Lead Validated";
$customers->validCustomer($_SESSION['username']);
header( "Location: admin.php?action=searchIsValid" );
}
function invalidCustomer(){
if (!$customers = customers::getById( (int)$_GET['customersId'] ) ){
echo "Error getting customer ID";
}
$results['formAction'] = "invalidCustomer";
$results['pageTitle'] = "Lead Invalidated";
$customers->invalidCustomer($_SESSION['username']);
header( "Location: admin.php?action=searchIsValid" );
}
And finally, the last piece of relevant code, the SQL part:
public function update() {
// Does the customer object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "customers::update(): Attempt to update a customer object that does not have its ID property set.", E_USER_ERROR );
// Update the customer
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE leads SET valid=:valid WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}
public function validCustomer($username) {
// Does the customer object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "customers::update(): Attempt to update a customer object that does not have its ID property set.", E_USER_ERROR );
// Update the customer
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
// if invalid
$sql = "UPDATE leads SET valid=1, reviewedby='$username' WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}
public function invalidCustomer($username) {
// Does the customer object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "customers::update(): Attempt to update a customer object that does not have its ID property set.", E_USER_ERROR );
// Update the customer
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
// if invalid
$sql = "UPDATE leads SET valid=0, reviewedby='$username' WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}
I think this is all the relevant code but, I probably forgot some so feel free to remind me, and thanks again for taking the time to read my post and offer assistance!
Related
I'm having this error and been trying to figure whats wrong for like 3 days straight with no luck:
Fatal error: Call to undefined method PDOStatement::bindValues() on line 92
My complete code
<?php
//CLASS TO HANDLE AD
class Ad
{
//Ad id from database
public $id = null;
//Ad client
public $client = null;
//Ad client login id
public $client_loginID = null;
//Ad video source
public $video = null;
//Ad banner source
public $banner = null;
//Ad cover source
public $cover = null;
//Ad mid video banner ad
public $midVideoBannerAd = null;
//Ad link
public $link = null;
//Ad click
public $clicks = null;
//Ad impressions
public $impressions = null;
//If ad is active
public $active = null;
//Sets the obect properties using the values in supplied array
public function __construct( $data=array() ){
if( isset ( $data['id'] ) ) $this->id = (int) $data['id'];
if( isset ( $data['client'] ) ) $this->client = $data['client'];
}
//Sets the object properties using the edit form post values in the supplied array
public function storeFormValues( $params ){
//Store all the parameters
$this->__construct( $params );
}
//Returns an Author Object matching the given id
public static function getById( $statement ){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM ad $statement";
$st = $conn->prepare( $sql );
$st->execute();
$row = $st->fetch();
$conn = null;
if( $row ) return new Ad( $row );
}
//Returns all (or range of) ad object in the db
public static function getList( $statement ){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM ad $statement";
$st = $conn->prepare( $sql );
$st->execute();
$list = array();
while( $row = $st->fetch() ){
$ad = new Ad( $row );
$list[] = $ad;
}
//Now get the total number of Ad that match the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
//Insert current Ad object into database and set its ID properties
public function insert(){
//Check if Ad object already has an id
//Insert the Ad
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO ad (client) VALUES ( :client )";
$st = $conn->prepare( $sql );
$st->bindValues( ":client", $this->client, PDO::PARAM_STR );
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
}
//Updates the current Ad in DB
public function update(){
//Check if Ad object has an id
if( !is_null ( $this->id ) ) trigger_error ( "Ad::update(): Attempt to update an Ad object that already has an ID set.", E_USER_ERROR );
//Updates the Ad
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE ad set client=:client, client_loginID=:client_loginID, video=:video, midVideoBannerAd=:midVideoBannerAd, banner=:banner, cover=:cover, link=:link, active=:active WHERE id=:id";
$st = $conn->prepare( $sql );
$st->bindValues( ":client", $this->client, PDO::PARAM_STR );
$st->bindValues( ":client_loginID", $this->client_loginID, PDO::PARAM_INT );
$st->bindValues( ":video", $this->video, PDO::PARAM_INT );
$st->bindValues( ":midVideoBannerAd", $this->midVideoBannerAd, PDO::PARAM_INT );
$st->bindValues( ":banner", $this->banner, PDO::PARAM_INT );
$st->bindValues( ":cover", $this->cover, PDO::PARAM_INT );
$st->bindValues( ":link", $this->link, PDO::PARAM_STR );
$st->bindValues( ":active", $this->active, PDO::PARAM_INT );
$st->bindValues( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}
//Delete current Ad from Database
public function delete(){
//Delete the Ad
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$st = $conn->prepare( "DELETE FROM ad WHERE id=:id" );
$st->bindValues( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}
}
And this is what's on line 92:
$st->bindValues( ":client", $this->client, PDO::PARAM_STR );
The method is called PDOStatement->bindValue() without the trailing "s"
see http://www.php.net/manual/en/pdostatement.bindvalue.php
I have tried everything I can possibly think of to get MSSQL to work with WAMP. Here is my code
<?php
$database = "appas";
$conn = new PDO( "sqlsrv:server=(IP) ; Database = $database", "sa1", "Password02");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$conn->setAttribute( PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 1 );
$query = 'select * from user';
// simple query
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row['first'] ."\n" );
}
echo "\n........ query for a column ............\n";
// query for one column
$stmt = $conn->query( $query, PDO::FETCH_COLUMN, 1 );
while ( $row = $stmt->fetch() ){
echo "$row\n";
}
echo "\n........ query with a new class ............\n";
$query = 'select * from user order by last';
// query with a class
class cc {
function __construct( $arg ) {
echo "$arg";
}
function __toString() {
return $this->DepartmentID . "; " . $this->Name . "; " . $this->GroupName;
}
}
$stmt = $conn->query( $query, PDO::FETCH_CLASS, 'cc', array( "arg1 " ));
while ( $row = $stmt->fetch() ){
echo "$row\n";
}
echo "\n........ query into an existing class ............\n";
$c_obj = new cc( '' );
$stmt = $conn->query( $query, PDO::FETCH_INTO, $c_obj );
while ( $stmt->fetch() ){
echo "$c_obj\n";
}
$stmt = null;
?>
I am getting this error
( ! ) Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\wamp\www\Register\configdb.php on line 3
( ! ) PDOException: could not find driver in C:\wamp\www\Register\configdb.php on line 3
Call Stack
# Time Memory Function Location
1 0.0004 255760 {main}( ) ..\configdb.php:0
2 0.0004 256608 __construct ( ) ..\configdb.php:3
I have no idea why it is not finding the driver - I have the DLL installed i think. I really am grasping at straws here if anyone knows my problem or could link a proper installation guide for MSSQL in WAMP 2.5 that would be fantastic.
http://imgur.com/8T4TasB
i am having some problem with my code, i try to simple insert some logs when user use search on my site.
Log should contains:
date in unix time -> using function for it
username -> its stored in Session
vin -> its string he post in input field
Top of the class
public $vin = null;
public function __construct( $data = array() )
{
if( isset( $data['vin'] ) )
$this->vin = stripslashes( strip_tags( $data['vin'] ) );
}
public function storeFormValues( $params )
{
$this->__construct( $params );
}
function code:
public function logFromSearch() {
$correct = false;
//$this->username = $_SESSION]['username'];
$date = new DateTime();
try {
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO logs(date, user, vin) VALUES(:date, :user, :vin)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "date", $date->getTimestamp(), PDO::PARAM_STR );
$stmt->bindValue( "user", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
echo $this->vin;
echo $date->getTimestamp();
$stmt->execute();
return "Everything is OKEY";
} catch( PDOException $e ) {
return $e->getMessage();
}
}
Its not importing anything to database dispite the fact it should in my opinion. I try to debug it so i found out my way of adding username from session is not possible way and also find out that this->vin and $date->getTimestamp() is returning actual data which it should insert to database so can somebody help me to find where is the problem? (in same class i am using other function for database connection and they work find so its not in setting up connecting / db name and host and so on because its in different file)
P.S. Errors : I dont get any errors just single notice Notice: Undefined property: Data::$username which is understandable.
I have an error and I don't know what I am doing wrong. Help, please, with this :)
So, I have a class named Activities and in it a I have few functions as here:
class Activities
{
public $content = null;
public function __construct( $data=array() ) {
if ( isset( $data['content'] ) ) $this->content = $data['content'];
}
public function storeFormValues ( $params ) {
$this->__construct( $params );
}
public static function getData() {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM exceptions WHERE name = 'atrakcje'";
$st = $conn->prepare( $sql );
$st->execute();
$row = $st->fetch();
$conn = null;
if ( $row ) return new Activities( $row );
}
public function update() {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE exceptions SET content=:content WHERE name = 'atrakcje'";
$st = $conn->prepare ( $sql );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->execute();
$conn = null;
}
}
And I have a function editActivities() in my little Admin Panel I made:
function editActivities() {
$results = array();
$results['pageTitle'] = "Edytuj Atrakcje";
$results['formAction'] = "editActivities";
if ( isset( $_POST['saveChanges'] ) ) {
$activities->storeFormValues( $_POST );
$activities->update();
header( "Location: admin.php?action=editActivities&status=changesSaved" );
} elseif ( isset( $_POST['cancel'] ) ) {
header( "Location: admin.php?action=editActivities" );
} else {
$results['activities'] = Activities::getData();
require( TEMPLATE_PATH . "/admin/editActivities.php" );
}
}
Also I have a HTML Form to make changes. But when I submit filled form I get an error:
Fatal error: Call to a member function storeFormValues() on a non-object in C:\xampp\htdocs\admin.php on line 285
285 line in my code is here:
if ( isset( $_POST['saveChanges'] ) ) {
$activities->storeFormValues( $_POST );
$activities->update();
I don't know what's going on. I have just the same code to edit other Articles and Users and I can't figure out what's bad here. Thanks for your help!
$activities is not instantiated, so you can't call one of its instance methods. You ought to instantiate it with
$activities = new Activities($_POST);
Also, in your static function getData you either return an Activities or NULL, depending on the value of $row. But this may make $results['activities'] sometimes an object and sometimes not, which is tricky to handle and might result in the same error being raised again.
add
$activities = new Activities();
$activities->storeFormValues( $_POST );
....
I'm in the process of learning to use OOP in php, I'm having a few issues with what is probably a really simple solution that I'm just not using the right search terminology to find.
I have my class
class user {
function getUser() {
if ($_SESSION['status'] == "authorized") {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM users WHERE username = :username";
$st = $conn->prepare( $sql );
$st->bindValue( ":username", $_SESSION['usernames'], PDO::PARAM_STR );
$st->execute();
$row = $st->fetch();
$conn = null;
return $row;
}
}
}
and then in my template file i'm calling the following
$user = new user();
echo $user->getUser->icon;
hopefully the bottom line shows what i'm trying to call, basically in the sql results I'm after just calling $row['icon']; directly from the return array.
is this possible? if so could someone point out the terminology i'm missing
Thanks in advance
If you are going to keep using that object I would do the following:
class user {
public $icon;
function getUser() {
if ($_SESSION['status'] == "authorized") {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT * FROM users WHERE username = :username";
$st = $conn->prepare( $sql );
$st->bindValue( ":username", $_SESSION['usernames'], PDO::PARAM_STR );
$st->execute();
$row = $st->fetch();
$conn = null;
$this->icon=$row;
}
}
}
Then you can use:
echo $user->icon;
Try the following :
print_r ($user->getUser);
If that returns an array, try it like this :
echo $user->getUser['icon'];
You should use it this way:
$userobj = new User();
$user = $userobj->getUser();
Now you have the fetched data in the $user variable and may output it at will:
echo $user['icon'];
My example should work with your existing code, and if you want to change the values in the future of the users, you just change the key in the echo statement: echo $user['someothervalue'];