Jump to content

sfia

Members
  • Posts

    21
  • Joined

  • Last visited

Everything posted by sfia

  1. ok with some help i got it working and doing what i want. <?php $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else if ($stream =='nursing') { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); mysqli_stmt_execute($stmt); } ?>
  2. ok, I get that part. I'm not sure why I was doing it I think i just thought I was preparing a statement. I actually removed all three mysqli_query and my query still works. But don't I bind my variables by doing this? ---> mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); I thought my sql queries just prepared statements with placeholders(?, ?, ?)
  3. <?php $username = $_POST['uid']; $email = $_POST['mail']; $password = $_POST['pwd']; $passwordRepeat = $_POST['pwd-repeat']; $date = $_POST['date2']; $stream = $_POST['relationship']; $sql1 = "INSERT INTO users (uidUsers, emailUsers, pwdUsers, relationship) VALUES (?, ?, ?, ?);"; $sql2 = "INSERT INTO Family1 (username, application_filed, relationship) VALUES (?, ?, ?);"; $sql3 = "INSERT INTO Family2 (username, application_filed, relationship) VALUES (?, ?, ?);"; mysqli_query($sql1, $conn); mysqli_query($sql2, $conn); mysqli_query($sql3, $conn); $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql3)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='doctoral'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { $hashedPwd = password_hash($password, PASSWORD_DEFAULT); mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $hashedPwd, $stream); mysqli_stmt_execute($stmt); header("Location: ../signup.php?signup=success"); exit(); ?> sorry, i added php tags to make it better readable. My queries work with parameters that I set up. <?php mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); ?> But i dont know how to make it work with that condition before executing query. perhaps I'm not using right logic here? <?php ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); ?>
  4. $username = $_POST['uid']; $email = $_POST['mail']; $password = $_POST['pwd']; $passwordRepeat = $_POST['pwd-repeat']; $date = $_POST['date2']; $stream = $_POST['relationship']; $sql1 = "INSERT INTO users (uidUsers, emailUsers, pwdUsers, relationship) VALUES (?, ?, ?, ?);"; $sql2 = "INSERT INTO Family1 (username, application_filed, relationship) VALUES (?, ?, ?);"; $sql3 = "INSERT INTO Family2 (username, application_filed, relationship) VALUES (?, ?, ?);"; mysqli_query($sql1, $conn); mysqli_query($sql2, $conn); mysqli_query($sql3, $conn); $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql3)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='doctoral'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { $hashedPwd = password_hash($password, PASSWORD_DEFAULT); mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $hashedPwd, $stream); mysqli_stmt_execute($stmt); header("Location: ../signup.php?signup=success"); exit(); I was wondering if someone could point me in the right direction. I have this code. They idea I had behind it is to insert values into different tables depending on variables being passed. So when user fills out a form and selects $stream="nursing" I want results to go to table 'users' and 'Family1', but not 'Family2' table. and if user selects $stream='doctoral' results should go to table 'users' and 'Family2', and not go to 'Family1' But with my query I get results go to both table and also users table. And there is no restriction to what users selects, variable $stream being passed no matter what it is. Is this the wrong way to go here? Did I completely mess up the logic?
  5. ok, I added $uid1 = $_SESSION['uid']; and than $sql = "SELECT id,username, date1 FROM datetable WHERE username = '$uid1'"; and that works!! Hopefully its the right way.
  6. <?php session_start(); $servername = ""; $username = ""; $password = ""; $dbname = ""; $connect = mysqli_connect($servername, $username, $password,$dbname); $sql = "SELECT id,username, date1 FROM datetable WHERE username = '".$_SESSION['uid']['username'] ."'"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = [ $row['id'], $row['username'], $row['date1'] $TotalTime ]; } echo json_encode($json_array); ?> Yes, session_start on each page. I played around a little and edit some things, I do get this now when run the var_dump($_SESSION); code. But my json format disappears. [] array(4) { ["id"]=> int(34) ["uid"]=> string(8) "bostin21" ["email"]=> string(18) "emaill123@gmail.com" ["user"]=> NULL } If i removed var_dump($_SESSION); than my query runs again.
  7. I just used echo "<pre>"; var_dump($_SESSION); echo "</pre>"; and it returns NULL. So I'm missing my session value somehow on this page.
  8. $sql = "SELECT id,username, date1, date2, total FROM datetable WHERE username = '".$_SESSION['uid']['username'] ."'"; Hello again, I ran into problem when trying to pull information for logged in user. I'm trying to display results for current user only who is signed in. Now I do have session_start(); and my query works if I replace '".$_SESSION['uid']['username'] ."'"; with 'specificusername' but when I run my current query displayed above it returns me all records with username = empty (I have some empty usernames for testing reasons). So that tells me that query cant find unique user name that is logged in. I don't get any errors and query still goes through. My datetable structure is My uid is defined in another document like so and uses another table called "users" session_start(); $_SESSION['id'] = $row['idUsers']; $_SESSION['uid'] = $row['uidUsers']; $_SESSION['email'] = $row['emailUsers']; header("Location: ../member.php?login=success"); exit(); This query works on another page where it displays logged in username in html document. <?php echo $_SESSION['uid']; ?> I have tried many different ways yesterday to modify my request ( WHERE username = $_SESSION['uid']; WHERE username = '".$_SESSION["uid"]) many other variations, but no matter what never returns logged in user information only empty username records. Can anyone see a problem with that query? or is it due to something else I'm missing? Sorry, maybe this goes in MySQL thread not here in PHP page. If someone can, please move it. Thanks.
  9. OK, thanks, i will try to set it up
  10. Thanks, its working now.
  11. Actually I got it figured now. Query was right. my column name is different on database side. I was getting HTTP 500 error.
  12. Hi, I was just trying to post form data to MySQL and trying to also post value of logged in user? but that code below doesnt work..Do you see a typo? or is it done some other way? $uid =$_SESSION[ "uid" ]; $sql = "INSERT INTO Date (uid) VALUES (:uid)"; $query= $conn->prepare( $sql ); $query->execute( array( ':uid'=>$uid) );
  13. I followed that link and used validateDate function below and now my script works!. thank you. Appreciate the help. Just trying to learn some of PHP. function validateDate($date, $format = 'Y-m-d') { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; }
  14. if (validDate($_POST['date2'], $format = 'Y-m-d')){ $date2 = date('Y-m-d', strtotime(trim($_POST['date2']))); } else { $date2 = NULL; } Like something like this? IDK that doesnt seem to work
  15. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $form=$_POST; $startdate =$form[ 'date1' ]; $enddate=$form[ 'date2' ]; $total=$form[ 'date3' ]; $sql = "INSERT INTO Datetable (startdate, enddate, total) VALUES (:startdate, :enddate, :total)"; $query= $conn->prepare( $sql ); $query->execute( array( ':startdate'=>$startdate, ':enddate'=>$enddate, ':total'=>$total ) ); I converted mysqli as suggested, and now using PDO and it seems to be working, but I'm completely stumped on how can i create a code to prevent empty values in form for date to pass to database as 0000-00-00. Could someone please help me with that? Or just point to similar thread I can read up on how to make that code? Also what is your opinion on PDO being slower and not as robust as mysqli? Thank you.
  16. $value0 = $_POST['date1']; $value1 = $_POST['date2']; $value2 = $_POST['date3']; $sql = "INSERT INTO Datetable (startdate,enddate, total) VALUES ('$value0','$value1', '$value2')" ; $result = mysqli_query($sql); Hi, I was wondering is it possible to not insert values in MySQL if form entry is left blank? Right now if I dont enter any values in form for my dates than MySQL entry shows 0000-00-00 but I need it not to show anything.
  17. Hi I'm trying to get this to work. I have known date in MySQL database entered as 2019-08-01 and I need to calculate from that date to current time. What I have is this code $date_ts = strtotime($row['date_ts']); //Timestamp $date_str = date("M-d-Y", $date_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days but my result showing as ==> 18367 days Could someone please help me get this to work so it shows the right number of days. thank you
  18. thanks so much guys. I will give it a try.
  19. <?php $servername = "localhost"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; $connect = mysqli_connect("localhost", "xxxxx", "xxxxx","xxxxx"); $sql = "SELECT * FROM solstice"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = $row; } /*echo '<pre>'; print_r('records:'); echo '</pre>';*/ echo json_encode($json_array); ?> Sorry, I don't know if i explained it right. But I would like to add calculation of time to that PHP code that I have. I think I'm doing the calculation correct already, but I'm stuck as how to have it show up along with my other json result from PHP. $Date1_ts = strtotime($row['Date1']); //Timestamp $Date1_str = date("M-d-Y", $Date1_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $Date1_ts)/(60*60*24)) . ' days';//Total in days $start_ts = strtotime($row['Date1']); $end_ts = strtotime($row['Date2']); $date_diff = ($end_ts - $start_ts); $Date3= ($date_diff>0) ? floor($date_diff/ (60*60*24)) : ''; // days difference from start to finish
  20. Hi, Im trying to calculate days between two dates and add that to the php code below. I have this code that pulls records from my database and outputs it as json. <?php $servername = "localhost"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; $connect = mysqli_connect("localhost", "xxxxx", "xxxxx","xxxxx"); $sql = "SELECT * FROM solstice"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = $row; } /*echo '<pre>'; print_r('records:'); echo '</pre>';*/ echo json_encode($json_array); ?> Basically I was thinking something like this but I don't know how I can add this to php above that produces json . This information is calculated on the fly. I need to add TotalTime and Date3 $Date1_ts = strtotime($row['Date1']); //Timestamp $Date1_str = date("M-d-Y", $Date1_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $Date1_ts)/(60*60*24)) . ' days';//Total in days $start_ts = strtotime($row['Date1']); $end_ts = strtotime($row['Date2']); $date_diff = ($end_ts - $start_ts); $Date3= ($date_diff>0) ? floor($date_diff/ (60*60*24)) : ''; // days difference from start to finish I would be really grateful if someone could help me with this. Thank you.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.