How to insert data via php to multiple mysql databases? -
i trying insert username, password, , email 1 database's table, , insert same username instead of password , email, set 1 (to notifications row). here's code:
<?php **$con= new mysqli("localhost","root","","users"); $con2 = new mysqli("localhost","root","","notification");** // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $hpassword = hash( 'sha512', $_post['password'] ); $eusername = mysqli_real_escape_string( $con, $_post['username'] ); $eemail = mysqli_real_escape_string( $con, $_post['email'] ); $fusername = str_replace(' ', '', $eusername); $check = "select * users username='$fusername'"; $result = mysqli_query($con, $check); $data = mysqli_fetch_array($result, mysqli_num); if($data[0] > 1) { echo "user in exists<br/>"; } else { **$con->query="insert users (username, password, email) values ('$fusername','$hpassword','$eemail')"; $con2->query="insert notification (username, notifications) values ('$fusername','1')";** if (mysqli_query($con,$con->query)) { if (mysqli_query($con,$con2->query)) { } else { } sleep(2); header("location:login.php"); } else { echo "error: username exists.<br/>"; } } mysqli_close($con); ?>
right now, submits data users database. not notification database.
in second mysqli_query call, use same connection $con
in first. instead use $con2
.
if (mysqli_query($con,$con->query)) { if (mysqli_query($con2,$con2->query)) // <--- used $con2 here { }
Comments
Post a Comment