php connection ERROR

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5

61 Replies - 1992 Views - Last Post: 15 May 2019 - 04:47 PM Rate Topic: -----

#31 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2881
  • View blog
  • Posts: 11,266
  • Joined: 03-December 12

Re: php connection ERROR

Posted 09 May 2019 - 09:00 AM

Really! You know code doesn’t match, and are asking why it breaks and says it doesn’t match?
Was This Post Helpful? 0
  • +
  • -

#32 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 09:42 AM

yes know why doesnt work...i dont know whitch variable i need to write here['']
Was This Post Helpful? 0
  • +
  • -

#33 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 11:14 AM

hey guys my php code now looks like that....am i on the right way or it is totaly wrong(i know that code dont work becouse of line 30) but i wanna know if i am on the right way or totaly wrong??


<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

$db=new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])&&($_POST['how_long'])&&($_POST['how_many'])&&($_POST['allien_description'])&&($_POST['what_they_did'])&&($_POST['fang_spotted'])&&($_POST['email'])&&($_POST['other']))
{

 	$firstName=$_POST['first_name'];
 	$lastname=$_POST['last_name'];
 	$when=$_POST['when_it_happend'];
 	$how=$_POST['how_long'];
 	$howMany=$_POST['how_many'];
 	$allien=$_POST['allien_description'];
 	$what=$_['what_they_did'];
 	$fang=$_['fang_spotted'];
 	$email=$_POST['email'];
 	$other=$_POST['other'];
 }
 


if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])&&($_POST['how_long'])&&($_POST['how_many'])&&($_POST['allien_description'])&&($_POST['what_they_did'])&&($_POST['fang_spotted'])&&($_POST['email'])&&($_POST['other']))
{
$sql="INSERT INTO allien_abduction(first_name,last_name,when_it_happend,how_long,how_many,allien_description,what_they_did,fang_spotted,other,email)
	VALUES('$firstName','$lastname','$when','$how','$howMany','$allien','$what','$fang','$other','$email');";
}
	$result=mysqli_query($db, $sql);
	if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])&&($_POST['how_long'])&&($_POST['how_many'])&&($_POST['allien_description'])&&($_POST['what_they_did'])&&($_POST['fang_spotted'])&&($_POST['email'])&&($_POST['other']))
{
	
echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when;
echo ' and were gone for ' . $how . '<br />';
echo 'Number of aliens: ' . $howMany . '<br />';
echo 'Describe them: ' . $allien . '<br />';
echo 'The aliens did this: ' . $what . '<br />';
echo 'Was Fang there? ' . $fang . '<br />';
echo 'Other comments: ' .$other . '<br />';
echo 'Your email address is ' . $email;
}




?>


Was This Post Helpful? 0
  • +
  • -

#34 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2881
  • View blog
  • Posts: 11,266
  • Joined: 03-December 12

Re: php connection ERROR

Posted 09 May 2019 - 11:17 AM

You have lots of pointless code. I have a meeting shortly, but I'll show you a cleaner way. In the meantime, look at the link on prepared statements.
Was This Post Helpful? 1
  • +
  • -

#35 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,115
  • Joined: 15-January 14

Re: php connection ERROR

Posted 09 May 2019 - 11:31 AM

There's no reason to have 3 if statements checking the same thing, just have one if statement with all of the code you want to run inside it.
Was This Post Helpful? 0
  • +
  • -

#36 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 11:58 AM

yes now i have oen if-statement but i steeel have a problem with echo variable
Was This Post Helpful? 0
  • +
  • -

#37 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6257
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: php connection ERROR

Posted 09 May 2019 - 12:32 PM

This is an example of how I would do something like this (note that this has not been tested)
<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

// An array keyed by the required fields on the form,
// with values representing the datatype for the bind.
// NOTE: This is assuming all the fields in that database are strings (VARCHAR) or (CHAR). If any are an integer, you should change the 's' to an 'i' for that field.
$requiredFields = [
   'first_name' => 's',
   'last_name' => 's',
   'when_it_happend' => 's',
   'how_long' => 's',
   'how_many' => 's',
   'allien description' => 's',
   'what_they_did' => 's',
   'fang_spotted' => 's',
   'email' => 's',
   'other' => 's'
];

// Go through each of the keys in $requiredFields and
// check if the field is set in the $_POST variable, if
// not, add it to the $missingFields array for return.
function getMissingFields()
{
   $missingFields = [];

   foreach(array_keys($requiredField) as $field)
   {
      if (empty($_POST[$field]))
      {
         array_push($field);
      }
   }

   return $missingFields;
}

// Build the SQL query based on the $requiredFields
function buildSqlQuery($requiredFields)
{
   // We need a number of ?s, one for each required field, separated by commas
   // First make an array filled with ?s
   $qArray = array_fill(-1, count($requiredFields), '?');
   // Make the array into a string of ?s, each separated by a comma
   $params = implode($qArray, ', ');
   // We need a list of the fields for saving.
   $fields = array_keys($requiredFields);
   // Make the array into a string of field names separated by a comma
   $fieldList = implode($fields, ', ');
   // Return the SQL query for perparation
   return "INSERT INTO allien_abduction ($fieldList) VALUES ($params)";
}

// Display the results back to the user
function displayResults()
{
   echo 'Thanks for submitting the form.<br />';
   echo 'You were abducted ' . $_POST['when_it_happnd'];
   echo ' and were gone for ' . $_POST['how_long'] . '<br />';
   echo 'Number of aliens: ' . $_POST['how_many'] . '<br />';
   echo 'Describe them: ' . $_POST['allien_description'] . '<br />';
   echo 'The aliens did this: ' . $_POST['what_they_did'] . '<br />';
   echo 'Was Fang there? ' . $_POST['fang_spotted'] . '<br />';
   echo 'Other comments: ' .$_POST['other'] . '<br />';
   echo 'Your email address is ' . $_POST['email'];
}

// Get any missing fields based on our required fields.
// If the array returned by the getMissingFields function is not empty,
// the user forgot to enter something.
$missingFields = getMissingFields($requiredFields);
if ( ! empty($missingFields) )
{
   // This will print out a list of the missing fields, separated by a comma.
   echo "Please enter all of the following fields: " . implode($missingFields, ', ') . "<br />";
   exit();
}

$mysqli = new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

// Create the prepared statement
$stmt = $mysqli->prepare(buildSqlQuery($requiredFields));
if ( ! $stmt )
{
   echo "Failed creating prepared statement: {$mysqli->error}<br />";
   exit();
}

// Bind our parameters to the data from the $_POST array
if ( ! $stmt->bind_param(implode(array_values($requiredFields), ','),
   $_POST['first_name'],
   $_POST['last_name'],
   $_POST['when_it_happend'],
   $_POST['how_long'],
   $_POST['how_many'],
   $_POST['allien_description'],
   $_POST['what_they_did'],
   $_POST['fang_spotted'],
   $_POST['email'],
   $_POST['other']
) )
{
   echo "Failed binding variables: {$stmt->error}<br />";
   exit();
}

// Execute the prepared statement
if ( ! $stmt->execute() )
{
   echo "Failed excuting query: {$stmt->error}<br />";
   exit();
}

// Get the number of rows that were inserted. It should be 1.
$recordsInserted = $stmt->affected_rows;

// We're done with the prepared statement
$stmt->close();

if ( $recordsInserted != 1)
{
   echo "Failed inserting into database! <br />";
   exit();
}

displayResults();

?>


This post has been edited by JackOfAllTrades: 09 May 2019 - 02:53 PM

Was This Post Helpful? 2
  • +
  • -

#38 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 02:11 PM

tnx jack for posting that code i very appreciated but this is more for profesionals not form begginers like i am...amm do you wana post some "beginer" code and by beginer code i meant code that i posted and i hope that you can fix it?
Was This Post Helpful? 0
  • +
  • -

#39 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15115
  • View blog
  • Posts: 60,493
  • Joined: 12-June 08

Re: php connection ERROR

Posted 09 May 2019 - 02:25 PM

What's wrong with that clearly written, well documented, and soundly explained code?

Be careful treading into the territory of asking folk to do your work for you.
Was This Post Helpful? 0
  • +
  • -

#40 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 02:32 PM

i dont wanna that folks doing work for me...just wanna know what i nedd to do that echo stuff will works in this code
<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

$db=new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])&&($_POST['how_long'])&&($_POST['how_many'])&&($_POST['allien_description'])&&($_POST['what_they_did'])&&($_POST['fang_spotted'])&&($_POST['email'])&&($_POST['other']))
{

 	$firstName=$_POST['first_name'];
 	$lastname=$_POST['last_name'];
 	$when=$_POST['when_it_happend'];
 	$how=$_POST['how_long'];
 	$howMany=$_POST['how_many'];
 	$allien=$_POST['allien_description'];
 	$what=$_['what_they_did'];
 	$fang=$_['fang_spotted'];
 	$email=$_POST['email'];
 	$other=$_POST['other'];
 
 



$sql="INSERT INTO allien_abduction(first_name,last_name,when_it_happend,how_long,how_many,allien_description,what_they_did,fang_spotted,other,email)
	VALUES('$firstName','$lastname','$when','$how','$howMany','$allien','$what','$fang','$other','$email');";


	$result=mysqli_query($db, $sql);

	
	
}
	
echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when;
echo ' and were gone for ' . $how . '<br />';
echo 'Number of aliens: ' . $howMany . '<br />';
echo 'Describe them: ' . $allien . '<br />';
echo 'The aliens did this: ' . $what . '<br />';
echo 'Was Fang there? ' . $fang . '<br />';
echo 'Other comments: ' .$other . '<br />';
echo 'Your email address is ' . $email;


?>



Was This Post Helpful? 0
  • +
  • -

#41 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,115
  • Joined: 15-January 14

Re: php connection ERROR

Posted 09 May 2019 - 03:08 PM

What specifically is not working? One problem is that you set the variables inside the if statement but try to print them outside the if statement. If it didn't go in that if statement, then none of those variables will be set. Another problem is that you're not setting them all correctly. Look carefully at the code you're using to set all of those variables.
Was This Post Helpful? 0
  • +
  • -

#42 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 09 May 2019 - 04:10 PM

problem is that in not print(echo) when i open just blank white space like that
http://shrani.si/f/1...BRp6F/blans.png

<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

$db=new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])&&($_POST['how_long'])&&($_POST['how_many'])&&($_POST['allien_description'])&&($_POST['what_they_did'])&&($_POST['fang_spotted'])&&($_POST['email'])&&($_POST['other']))
{

 	$firstName=$_POST['first_name'];
 	$lastname=$_POST['last_name'];
 	$when=$_POST['when_it_happend'];
 	$how=$_POST['how_long'];
 	$howMany=$_POST['how_many'];
 	$allien=$_POST['allien_description'];
 	$what=$_POST['what_they_did'];
 	$fang=$_POST['fang_spotted'];
 	$email=$_POST['email'];
 	$other=$_POST['other'];
 
 



$sql="INSERT INTO allien_abduction(first_name,last_name,when_it_happend,how_long,how_many,allien_description,what_they_did,fang_spotted,other,email)
	VALUES('$firstName','$lastname','$when','$how','$howMany','$allien','$what','$fang','$other','$email');";


	$result=mysqli_query($db, $sql);

	
	

	
echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when;
echo ' and were gone for ' . $how . '<br />';
echo 'Number of aliens: ' . $howMany . '<br />';
echo 'Describe them: ' . $allien . '<br />';
echo 'The aliens did this: ' . $what . '<br />';
echo 'Was Fang there? ' . $fang . '<br />';
echo 'Other comments: ' .$other . '<br />';
echo 'Your email address is ' . $email;

}




?>




Was This Post Helpful? 0
  • +
  • -

#43 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,115
  • Joined: 15-January 14

Re: php connection ERROR

Posted 09 May 2019 - 05:05 PM

If it's not printing anything then your if statement is not true. So, either that variable is not set, or one of the other values is empty. When you do this:

if(isset($_POST['first_name'])&&($_POST['last_name'])&&($_POST['when_it_happend'])...

it's the same as this:

if(isset($_POST['first_name']) && ($_POST['last_name'] != '') && ($_POST['when_it_happend'] != '')...

So you're not checking if all of the variables are set, you are checking if they were filled out in the form. If any of them is blank then the if statement is false.
Was This Post Helpful? 0
  • +
  • -

#44 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 22-October 18

Re: php connection ERROR

Posted 10 May 2019 - 03:39 AM

stell nothing

<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

$db=new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

if(isset($_POST['first_name'])&&($_POST['last_name']!='')&&($_POST['when_it_happend']!='')&&($_POST['how_long']!='')&&($_POST['how_many']!='')&&($_POST['allien_description']!='')&&($_POST['what_they_did']!='')&&($_POST['fang_spotted']!='')&&($_POST['email']!='')&&($_POST['other']!=''))
{

 	$firstName=$_POST['first_name'];
 	$lastname=$_POST['last_name'];
 	$when=$_POST['when_it_happend'];
 	$how=$_POST['how_long'];
 	$howMany=$_POST['how_many'];
 	$allien=$_POST['allien_description'];
 	$what=$_POST['what_they_did'];
 	$fang=$_POST['fang_spotted'];
 	$email=$_POST['email'];
 	$other=$_POST['other'];
 
 



$sql="INSERT INTO allien_abduction(first_name,last_name,when_it_happend,how_long,how_many,allien_description,what_they_did,fang_spotted,other,email)
	VALUES('$firstName','$lastname','$when','$how','$howMany','$allien','$what','$fang','$other','$email');";


	$result=mysqli_query($db, $sql);

	
	

	
echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when;
echo ' and were gone for ' . $how . '<br />';
echo 'Number of aliens: ' . $howMany . '<br />';
echo 'Describe them: ' . $allien . '<br />';
echo 'The aliens did this: ' . $what . '<br />';
echo 'Was Fang there? ' . $fang . '<br />';
echo 'Other comments: ' .$other . '<br />';
echo 'Your email address is ' . $email;

}




?>



Was This Post Helpful? 0
  • +
  • -

#45 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2881
  • View blog
  • Posts: 11,266
  • Joined: 03-December 12

Re: php connection ERROR

Posted 10 May 2019 - 04:54 AM

Not a fan of mysqli functionality, but...

<?php
$dbuser='root';
$dbpass='';
$db='prvadb';

$db=new mysqli('localhost',$dbuser,$dbpass,$db) or die("unable to connect");

if($_SERVER['REQUEST_METHOD'] == 'POST') {
	$sql="INSERT INTO allien_abduction(first_name,last_name,when_it_happend,how_long,how_many,allien_description,what_they_did,fang_spotted,other,email)
	VALUES(?,?,?,?,?,?,?,?,?,?);";

	$stmt = $db->prepare($sql);
	$stmt->bind_param("ssssssssss", $firstname, $lastname, $email);
        $stmt->execute();

	echo 'Thanks for submitting the form.<br />';
	echo 'You were abducted ' . htmlentities($_POST['when_it_happend']);
	echo ' and were gone for ' . htmlentities($_POST['how_long']) . '<br />';
	echo 'Number of aliens: ' . htmlentities($_POST['how_many']) . '<br />';
	echo 'Describe them: ' . htmlentities($_POST['allien_description']) . '<br />';
	echo 'The aliens did this: ' . htmlentities($_POST['what_they_did']) . '<br />';
	echo 'Was Fang there? ' . htmlentities($_POST['fang_spotted']) . '<br />';
	echo 'Other comments: ' . htmlentities($_POST['other']) . '<br />';
	echo 'Your email address is ' . htmlentities($_POST['email']);
}



This excludes the part of spelling alien incorrectly.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5