-2

I've tried things like setting a $var to the MySQL information (such as name or password), then pulling it with PHP, but I cannot figure the PHP out to do so. I use the server connect code as well. I have checked ASP.net out, but don't find any information on altering HTML text to equal SQL information. How might I go about accomplishing this?

One example: The account balance on my website is displayed in the bottom right corner, but I need to pull the balance from a column of my table from my database. What code would I use to set that paragraph element to the server value of account_balance?

My PHP/SQL for Registration:

<?php
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " .     mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

$email = $_POST['email'];
$emailConfirm = $_POST['confirmemail'];
$password =  $_POST['password'];
$passwordConfirm = $_POST['confirmpassword'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$countryCode = $_POST['countrycode'];
$phoneNumber = $_POST['phone'];
$address = $_POST['address'];
$zipCode = $_POST['zipcode'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$dateOfBirth = $_POST['birthdate'];
$registerDate = "CURDATE()";
$registerTime = "CURTIME()";
$paymentMethod = $_POST['moneymethod'];
if($email == $emailConfirm && $password == $passwordConfirm){
if(!empty($email) && !empty($_POST['confirmemail']) && !empty($password) && !empty($_POST['confirmpassword']) && !empty($fname) && !empty($lname) && !empty($countryCode) && !empty($phoneNumber) && !empty($address) && !empty($zipCode) && !empty($city) && !empty($state) && !empty($country) && !empty($paymentMethod)){
  $query = "INSERT INTO user_information (email,password,f_name,l_name,country_code,phone_number,address,city,state,zip_code,country,money_option,join_date,join_time) VALUES ('$email','$password','$fname','$lname','$countryCode','$phoneNumber','$address','$city','$state','$zipCode','$country','$paymentMethod','$registerDate','$registerTime')";
$data = mysql_query ($query)or die(mysql_error());
if($data){
  header( 'Location: http://www.madmater.com/register/success.php' );
}else{
  echo "Unknown Error!";
}
}else{
  echo 'Please fill out all required fields before completing your registration!';
}
}else{
  echo 'Your passwords or emails do not match!';
}

?>

I have only tried a simple PHP command, as it is the only thing I could think of, being new to MySQL.

$fname = $_POST['fname']; //From form
echo "<script type="text/javascript">document.getElementById("fname-holder").innerHTML($fname);</script>";

Even a hint of where to start would help, and thank you in advanced.

Austin Bunker
  • 53
  • 1
  • 3
  • 13

3 Answers3

1

Use form submit your values,

In that form in action give register.php

echo "<pre>";
print_r($_post);
echo"</pre>;

use that you got value then $name= $_post['name']

$query = "INSERT INTO "Tablename" set name= '".$name."',addeddate=now();

This way you can write.

cuSK
  • 809
  • 11
  • 26
priya
  • 11
  • 2
1

There is no sql code in this example. Besides the error i found in this code of PHP for ' is not used.

$fName = $_POST['fname'] //This is from my register form
echo "<script type='text/javascript'>document.getElementById('fname-field').innerHTML(".$fname.");</script>";

and try to give some insert/update code also.

1

Since I don't know which row your account balance is called, I've used account_balance as an example in order to get you started.

Here is a basic method to retrieve information from your database:

$fname = stripslashes($_POST['fname']);
$fname = mysql_real_escape_string($_POST['fname']);

// or if DB connnection is required
// $fname = mysql_real_escape_string($_POST['fname'], $db);

$query = "SELECT * FROM user_information WHERE f_name = '$fname'";

    while($row = mysql_fetch_array($query)){

        $user = $row['f_name'];
        $balance = $row['account_balance'];

        echo "Username: " . $user;
        echo "<br>";
        echo "Balance: " . $balance;
}

or mysql_fetch_assoc() depending on what method you wish to use.

For more information on MySQL's SELECT, visit:

You can also try:

$row = mysql_fetch_array($query);
    foreach($row as $r) {
    echo $r . "<br>";
}

or:

$row = mysql_fetch_assoc($query);
    foreach($row as $r) {
    echo $r . "<br>";
}

Footnotes:

I have to state that your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141