mysql - Google Cloud SQL & PHP tutorial isn't editing database? -
mysql - Google Cloud SQL & PHP tutorial isn't editing database? -
i'm learning how utilize google cloud sql , trying connect database using php. i'm next google instructions here.
i created 3 files (guestbook.php
, sign.php
, , app.yaml
) , copied code presented in article. in order connect database, used pdo_mysql
variant.
in guestbook.php
, sign.php
, in if
statement: replaced <your-project-id>
, <your-instance-name>
appropriate. left ''
password empty.
in guestbook.php
, sign.php
, in else
statement: replaced 127.0.0.1
, <password>
appropriate. left 3306
is.
in app.yaml
: replaced <your-application-name>
appropriate.
i saved 3 files in local directory , deployed using googleappenginelauncher
.
when loaded first page, looked fine. typed in , nail submit
. got redirected page /guestbook
, told:
error: not found requested url /guestbook not found on server.
i new web apps , php in general. have no thought error message asking me do. i've uploaded files google server using app launcher. i'm confident i've followed instructions closely.
some things i've tried doing:
in guestbook.php
, sign.php
, filled in ''
appropriate password.
in guestbook.php
, sign.php
, deleted default 3306
.
in guestbook.php
, sign.php
, deleted if
statement , used content of else
statement.
in app.yaml
: added -url: /guestbook
.
in guestbook.php
, sign.php
, switched pdo_mysql
mysqli
.
none of attempts above did solve problem. attempts 1 resulted in message unable connect.
.
edit:
in sign.php
, changed header('location: '."/guestbook");
header('location: '."/");
. seems have gotten rid of url not found
error, seems nil in database beingness altered. php doing nil alter database.
what doing wrong? how can prepare problem , connect, read, , write database?
figured out. guestbook php code broken, plain , simple. seek redirect (using location header) after output , tags, illegal.
the result of is, if you're not logged in, code supposed redirect login page, doesn't. , when seek post invitee book, "$user" null, page fails.
oh yeah, , sign.php page redirects /guestbook, correctly identified doesn't exist. , redirects @ top of sign.php code, create code afterwards nothing. here's fixed code.
guestbook.php:
<?php utilize google\appengine\api\users\user; utilize google\appengine\api\users\userservice; $user = userservice::getcurrentuser(); if ($user === null) { header('location: ' . userservice::createloginurl($_server['request_uri'])); } ?> <html> <body> <h2>guestbook entries</h2> <?php // create connection. echo 'hello, ' . htmlspecialchars($user->getnickname()) . '<br>'; $db = null; if (isset($_server['server_software']) && strpos($_server['server_software'],'google app engine') !== false) { // connect app engine. try{ $db = new pdo(mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=guestbook', 'root', ''); }catch(pdoexception $ex){ die(json_encode( array('outcome' => false, 'message' => 'unable connect.') ) ); } } else { // connect development environment. try{ $db = new pdo('mysql:host=127.0.0.1:3306;dbname=guestbook', 'root', '<password>'); }catch(pdoexception $ex){ die(json_encode( array('outcome' => false, 'message' => 'unable connect') ) ); } } seek { // show existing guestbook entries. foreach($db->query('select * entries') $row) { echo "<div><strong>" . $row['guestname'] . "</strong> wrote <br> " . $row['content'] . "</div>"; } } grab (pdoexception $ex) { echo "an error occurred in reading or writing guestbook."; } $db = null; ?> <h2>sign guestbook</h2> <form action="/sign" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="sign guestbook!!"></div> </form> </body> </html>
and sign.php
<?php utilize google\appengine\api\users\user; utilize google\appengine\api\users\userservice; $user = userservice::getcurrentuser(); if (!$user) { die("not logged in"); } $db = null; if (isset($_server['server_software']) && strpos($_server['server_software'],'google app engine') !== false) { // connect app engine. try{ $db = new pdo('mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=guestbook', 'root', ''); }catch(pdoexception $ex){ die(json_encode( array('outcome' => false, 'message' => 'unable connect.') ) ); } } else { // connect development environment. try{ $db = new pdo('mysql:host=127.0.0.1:3306;dbname=guestbook', 'root', '<password>'); }catch(pdoexception $ex){ die(json_encode( array('outcome' => false, 'message' => 'unable connect') ) ); } } seek { if (array_key_exists('content', $_post)) { $stmt = $db->prepare('insert entries (guestname, content) values (:name, :content)'); $stmt->execute(array(':name' => htmlspecialchars($user->getnickname()), ':content' => htmlspecialchars($_post['content']))); $affected_rows = $stmt->rowcount(); // log $affected_rows. } } grab (pdoexception $ex) { // log error. } $db = null; header('location: '."/"); ?>
php mysql google-app-engine google-cloud-sql
Comments
Post a Comment